Vivek Dhiman
Vivek Dhiman

Reputation: 1997

If equality check in handle bar

I have handlebar code in jsp file. When I use ${var} is shows me true/false. Now I need to use this in same file under handlebar script like below :

    ${flag}  // true or false
    <script type="text/x-handlebars-template" id="tpl-bla">
        {{#each item}}
        <div>
        {{#if this.Options}}
                {{#this.ida}}
                    {{#unless @index}}
                        {{#if icon}}
                        {{#if flag}}
                                <img>
                        {{/if}}
                        {{/if}}
                    {{/unless}}
                {{/this.ida}}

............ More Code...............

flag is the string value. Does anyone knows how to solve this? I want to display img when flag is equals "true".

What I tried and didn't work, adding below snippet in script tag:

 Handlebars.registerHelper('ifcond', function(v1, v2, options) {
            if(v1 === v2) {
                return options.fn(this);
              }
            }); 

Adding below line :

{{#ifcond flag === 'true'}}
           <img />
{{/ifcond}}

Upvotes: 0

Views: 1431

Answers (1)

Marcos Casagrande
Marcos Casagrande

Reputation: 40444

=== is not valid handlebars, remove it and your block will work.

Change:

{{#ifcond flag === 'true'}}
   <img />
{{/ifcond}}

To:

{{#ifcond flag 'true'}}
    <img />
{{/ifcond}}

You can improve your helper, adding {{else}} support:

Handlebars.registerHelper('ifcond', function(v1, v2, options) {
    if (v1 === v2)
        return options.fn(this);
    return options.inverse(this); //Will enter else block
});

You can then use:

{{#ifcond flag 'true'}}
   <img />
{{else}}
   <!-- I'm false -->
{{/ifcond}}

Upvotes: 1

Related Questions