How can I add an hr element from every 3 of my cycle? .. using Handlebarsjs?

I have an array of elements, I add them all to the form element. everything is working fine. But I canโ€™t add an โ€œhrโ€ element to each of my 3 shortcuts. I have 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 it doesnโ€™t work .. can someone suggest me the right way, please ..?

Thank you in advance

+4
source share
1 answer

First auxiliary register, for example showHr

 Handlebars.registerHelper("showHr", function(index_count,block) { if(parseInt(index_count)%3=== 0){ return block.fn(this);} }); 

Now in the template

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

Or, if you want, you can write a general helper, contact http://doginthehat.com.au/2012/02/comparison-block-helper-for-handlebars-templates/

Change comment.

 Handlebars.registerHelper("moduloIf", function(index_count,mod,block) { if(parseInt(index_count)%(mod)=== 0){ return block.fn(this);} }); 

Given the start of the index 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}} 
+10
source

Source: https://habr.com/ru/post/1486631/


All Articles