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.

conditional in the index

  • #5032
    Avatar photo[anonymous]

    Hello,

    I want to create a conditional in the index to show only posts from a specific category.

    I am attacking the problem in the following way:

    {{#each tags}}
    {{#if tag.name '==' "news"}}
    
    • <button class="filter__item" data-filter=".{{slug}}">{{name}}</button>
    {{/if}} {{/each}}

    Could you tell me which is the proper way? It is my first contact with Handlebars.

    Thank you.

    #5033
    Avatar photoBob

    You should use {{#getPostsByTags}} helper https://getpublii.com/dev/getpostsbytags-helper/

    #5034
    Avatar photo[anonymous]

    Thanks for your quick response.
    Could you tell me what is wrong with my code?

    #5035
    Avatar photoBob

    Your code snippet returns the tag name, not a list of posts.

    #5036
    Avatar photo[anonymous]

    I am more interested in the declaration of the conditional, than in receiving the posts. This conditional is correct? So is this correct?

    {{#each tags}}
    {{#if tag.name '==' "news"}}
       // .. any block
    {{/ if}}
      {{/ each}}
    
    #5037
    Avatar photoBob

    It’s not, you should use the {{checkIf}} helper, insted of {{if}}, e.g.:

    {{#each tags}}
       {{#checkIf tag.name '==' "news"}}
          // .. any block
       {{/checkIf}}
    {{/ each}}

     
    I’m not sure where you are, so you may need to change the context. When you include ../ segments in your path, handlebars will change back into the parent context.
    So the code above should look like this:

    {{#each tags}}
       {{#checkIf ../tag.name '==' "news"}}
          // .. any block
       {{/checkIf}}
    {{/ each}}
    #5038
    Avatar photo[anonymous]

    Thank you really for your two notes.
    I close then.