简体   繁体   中英

Handlebars JS if statement not working

I'm using handlebars templating engine to output a load of data.

I'm trying to use an if statement to only show the first ten iritations. Is this possible?

Below is what I have so far.

{{#if @index > 10}}
    <div style="display:none;">
{{else}}
    <div>
{{/if}}

You could register a helper

Handlebars.registerHelper("onlyten",function(arr,options) {
    if(options.inverse && !arr.length)
        return options.inverse(this);

    return arr.map(function(item,index) {
        item.$dontshow = index > 10;
        return options.fn(item);
    }).join('');
});

and use it like

{{#onlyten yourArray}}
    <div{{#if $dontshow}} style="display: none;"{{/if}}></div>
{{/onlyten}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM