Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars conditional class attributes

Using EmberJS/Handlebars, how can I add a class to an element, only if a condition is true?

<div {{#if isSearching}}class="foo"{{/if}}></div>

Like that, but less pseudocode and more reality.

like image 618
alt Avatar asked Sep 24 '13 21:09

alt


People also ask

How do you iterate objects in Handlebars?

To iterate over an object with JavaScript and Handlebars. js, we can use the #each keyword. to add the Handlebars script and the template for looping through each entry in data . We use #each this to loop through each array property.

What are helpers in Handlebars?

Helpers can be used to implement functionality that is not part of the Handlebars language itself. A helper can be registered at runtime via Handlebars. registerHelper , for example in order to uppercase all characters of a string.

How do you use ternary Handlebars?

The if helper can be used as a ternary operator by passing three arguments to it. In the following example, a button has a default value of "Save Changes" , but when model. isSaving is true, then the value temporarily changes to Saving... .


1 Answers

{{bind-attr}} is deprecated. You can use the new much nicer bound attribute syntax in Ember 1.10 and above, like so:

<div class="{{if isSearching 'foo'}}"></div>    
like image 52
Adam Cooper Avatar answered Nov 25 '22 02:11

Adam Cooper