Django has a template tag called a loop: https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#cycle.
Example:
{% for o in some_list %} <tr class="{% cycle 'row1' 'row2' %}"> ... </tr> {% endfor %}
Exit:
<tr class="row1">...</tr> <tr class="row2">...</tr> <tr class="row1">...</tr> <tr class="row2">...</tr>
How do you implement this type of functionality in Handlebars.js?
I found at http://thinkvitamin.com/code/handlebars-js-part-2-partials-and-helpers/
Handlebars.registerHelper("stripes", function(array, even, odd, fn) { if (array && array.length > 0) { var buffer = ""; for (var i = 0, j = array.length; i < j; i++) { var item = array[i]; // we'll just put the appropriate stripe class name onto the item for now item.stripeClass = (i % 2 == 0 ? even : odd); // show the inside of the block buffer += fn(item); } // return the finished buffer return buffer; } }); {{#stripes myArray "even" "odd"}} <div class="{{stripeClass}}"> ... code for the row ... </div> {{/stripes}}
Here is what I came up with:
Handlebars.registerHelper('cycle', function(value, block) { var values = value.split(' '); return values[block.data.index % (values.length + 1)]; }); {{#each users}} <tr class="{{cycle 'alternate'}}"> <tr class="{{cycle 'odd even'}}"> {{/each}}
Source: https://habr.com/ru/post/1399285/More articles:Using excess memory in matplotlib imshow - pythonUIImagePickerController how to hide the flip camera button? - objective-ccss display: what? - arrange boxes one by one - cssWhat type of "assert" should rspec use to verify prerequisites? - ruby-on-rails"Unhandled event Loop Exception" when executing something in Android XML - androidRuby Hash Initializers - ruby | fooobar.comHow to read the contents of the address bar after '?' mean and embed value in HTML? - javascripthttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1399289/how-can-i-check-if-the-current-window-is-top-or-parent-via-php&usg=ALkJrhjgTpCdU4ii_cKaRnlgfIJ4BJ4t0QOrganizing various queries in Node.js - javascriptIPad canvas flicker when using - ipadAll Articles