Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

How do you do math with checkIf helper?

  • #8095
    Avatar photo[anonymous]

    I want to insert a div with a class of row every 3 posts pulled from {{#each posts}} for my grid system but I’m failing to figure out how to do it.

    Currently I’m using the following on my index page:

    {{#if featuredPosts}}
      {{#each featuredPosts}}
        {{#checkIf @index '==' 0}} 
        
    {{/checkIf}} {{#checkIf @index '==' 3}} 
    {{/checkIf}} {{#checkIf @index '==' 2}} 
    {{/checkIf}} {{#checkIf @index '==' 5}} 
    {{/checkIf}} {{/each}} {{/if}}

    Of course if the count doesn’t match up the will not be created which will create broken html. So I need to be able to get the featured posts count and do math inside of the checkIf helper for example: {{#checkIf {{math @index ‘/’ 3}} ‘==’  1}}. I tried this and I get an error.

    #8185
    Avatar photo[anonymous]

    Your syntax with math is invalid – you should try to use subexpressions syntax: https://handlebarsjs.com/guide/expressions.html#subexpressions – maybe it will help with your case

    #8386
    Avatar photo[anonymous]
    <header class="featured">
      
    {{#if featuredPosts}} {{#each featuredPosts}} {{#checkIf (math @index '%' 3) '==' 0}}
    {{/checkIf}} {{/each}} {{/if}}
    </header>

    Thanks for the help, here is my final code in case anyone is interested.