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
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/
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}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With