-2

Hi I want to select the current month as default in the below code in Ember 1.10, current month is a variable in the controller:

currentMonth: moment().format("M"),
allMonths: moment.months()

////// HBS ////////////

<select id="selectMonth">
    {{#each month in allMonths}}
        {{#if currentMonth === month}}
            <option selected="selected" value='{{_view.contentIndex}}'>{{month}}</option>   
        {{else}}
            <option value='{{_view.contentIndex}}'>{{month}}</option>
        {{/if}}
    {{/each}}
</select>   

I am getting Error for this. what is the other way we can add a if condition in this scenario.

kusha
  • 1
  • 2
  • Try to take a look to this answer it may help you to understand the #if condition : https://stackoverflow.com/questions/48542688/handlebar-if-condition-not-working/48545293#48545293 – Christophe Feb 07 '18 at 11:26
  • 3
    Possible duplicate of [Logical operator in a handlebars.js {{#if}} conditional](https://stackoverflow.com/questions/8853396/logical-operator-in-a-handlebars-js-if-conditional) – Gibin Ealias Feb 07 '18 at 12:48
  • 1
    take a look at https://www.npmjs.com/package/ember-truth-helpers – Nicolas Feb 08 '18 at 23:09

1 Answers1

0

Ember uses Handlebars, a templating language designed to be as logicless as possible. The {{#if}} helper just only accepts a boolean variable. For instance:

{{#if isCurrentMonth}}
    //current month logic
{{else}}
    //non current month logic
{{/if}}

Although a logicless design sounds awesome, you quickly stumble upon problems when using the {{#each}} helper. So most of the community quickly left the idea of completely logicless templates. Nowadays many people use the ember-truth-helpers.

Just install them:

ember install ember-truth-helpers

And use them:

{{#if (eq currentMonth month)}}

P.S. Your Ember version is very old; update to 2.18 or 2.16 LTS.

Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78