Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars.js if block helper ==

How would you change the following code to make it work? The problem is the this == 'some message' expression:

<ul>
  {{#each errors}}
    {{#if (this == 'some message') }}
    <li>Status</li>
    {{else}}
    <li>{{this}}</li>
    {{/if}}
  {{/each}}
</ul>
like image 279
Marek Příhoda Avatar asked Feb 26 '13 11:02

Marek Příhoda


People also ask

What is helper in Handlebars?

A Handlebars helper call is a simple identifier, followed by zero or more parameters (separated by a space). Each parameter is a Handlebars expression that is evaluated exactly the same way as described above in "Basic Usage": template {{firstname}} {{loud lastname}}

How do you make Handlebars for helpers?

To create custom helpers in your theme just create a helpers. js file in the theme directory; here you can add your custom helper functions. Below there is an example of code which creates two super simple custom helpers which accept one argument: /* * Custom theme helpers for Handlebars.

Is there else if in Handlebars?

Handlebars supports {{else if}} blocks as of 3.0. 0. is the point in not having an elsif (or else if) in handlebars that you are putting too much logic into your template.

How do you use unless Handlebars?

You can use the unless helper as the inverse of the if helper. Its block will be rendered if the expression returns a falsy value. If looking up license under the current context returns a falsy value, Handlebars will render the warning. Otherwise, it will render nothing.


3 Answers

The easiest thing would be to add a custom if_eq helper:

Handlebars.registerHelper('if_eq', function(a, b, opts) {
    if(a == b) // Or === depending on your needs
        return opts.fn(this);
    else
        return opts.inverse(this);
});

and then adjust your template:

{{#if_eq this "some message"}}
    ...
{{else}}
    ...
{{/if_eq}}

Demo: http://jsfiddle.net/ambiguous/d4adQ/

If your errors entries weren't simple strings then you could add "is this some message" flags to them and use a standard {{#if}} (note that adding a property directly to a string won't work that well):

for(var i = 0; i < errors.length; ++i)
    errors[i] = { msg: errors[i], is_status: errors[i] === 'some message' };

and:

{{#if is_status}}
    <li>Status</li>
{{else}}
    <li>{{msg}}</li>
{{/if}}

Demo: http://jsfiddle.net/ambiguous/9sFm7/

like image 193
mu is too short Avatar answered Oct 10 '22 13:10

mu is too short


Old question, but if you use Elving's Swag Handlebars helpers library, you can use the helpers is and isnt.

like image 11
Matt Fletcher Avatar answered Oct 10 '22 13:10

Matt Fletcher


This can also be achieved by using Handlebars Subexpressions.

Template -

    <script id="tmplStatus" type="text/x-handlebars">
        <ul>
            {{#each errors}}
                {{#if (is_status this 'some message')}}
                    <li>Status</li>
                {{else}}
                    <li>{{this}}</li>
                {{/if}}
            {{/each}}
        </ul>
    </script>

Javascript -

var errors = [
        'Where is pancakes house?',
        'some message',
        'One cent stamp'
    ];

    Handlebars.registerHelper('is_status', function(msg, matchMsg, options) 
    {
        if(msg === matchMsg)
            return true;
        else
            return false;
    });

    var tmplStatus = Handlebars.compile($('#tmplStatus').html()),
        domStatus = tmplStatus({ errors: errors });
    $('body').append(domStatus);

Working Demo : http://jsfiddle.net/techgeeek/b99qwtpw/

like image 7
Neha Avatar answered Oct 10 '22 13:10

Neha