Sign in

How do you do math with checkIf helper?

  • This topic has 2 replies, 2 voices, and was last updated 4 days, 14 hours ago by .
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #8095
    Avatar photoTravsVoid

    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 photoTomasz Dziuda

    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

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #8386
    Avatar photoTravsVoid
    <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.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.