Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add a hr element of each 3 of my looping array..using Handlebarsjs?

I have a array of elements, i am appending them all to my form element. all works fine. But i am not able to add a "hr" element to each of my 3rd label.. i tried this.

<script id="locale-template" type="text/x-handlebars-template">
        {{#each this}}
                {{#if @index % 3 === 0 }}
                    <hr/>
        {{/if}}
        <label><input type="checkbox" /> {{name}} </label>
        {{/each}}
    </script>

But not works.. can any one suggest me the correct way please..?

Thanks in advance

like image 414
3gwebtrain Avatar asked Dec 26 '22 01:12

3gwebtrain


1 Answers

First register helper like showHr

Handlebars.registerHelper("showHr", function(index_count,block) {

  if(parseInt(index_count)%3=== 0){
    return block.fn(this);}


});

Now In Template

{{#showHr @index}}
    <hr/>
{{/showHr}}

Or if you want you can write generic helper refer http://doginthehat.com.au/2012/02/comparison-block-helper-for-handlebars-templates/

Edit for comment question

Handlebars.registerHelper("moduloIf", function(index_count,mod,block) {

  if(parseInt(index_count)%(mod)=== 0){
    return block.fn(this);}
});

Considering index start from 0

// If index is 0 open div
// if index is 3 means open a div
{{#moduloIf @index 0}}
 <div>
{{/moduloIf}}
{{#moduloIf @index 3}}
 <div>
{{/moduloIf}}

 {{name}}
 // if index+1 is  modulo 3 close div
 {{#moduloIf @index+1 3}}
 </div>
 {{/moduloIf}}
like image 170
Amar Avatar answered May 22 '23 23:05

Amar