Search code examples
dust.js

Dust templating, check if iteration is divisible by a number


Trying to check to see if an iteration is divisible by a number, and wrap it in a <div> if it is, but I can't seem to get it to work. Is there even a way to do this with Dust?

{#sitemap.items}
    {@if cond="$idx % 2"}
        <div class="row">
    {/if}
        <div class="sitemap-column col-6">
            {#.}
            //do stuff with the object

            {/.}
        </div>

    {@if cond="sitemap.count % 2"}
        </div>
    {/if}            
{/sitemap.items}

Solution

  • The easiest way is to use the math helper like this:

    {#items}
      {@math key=$idx method="mod" operand=2}
        {@eq value=0}<div class="row">{/eq}
      {/math}
    {/items}