If tag == name on post and tag page

Hi,

I have a sidebar which is shown on all pages. I would like to query if take == name and if so return something.

{{#is "tag, post"}}
    {{#tag}}
        {{#has slug="test"}}same content{{/has}}
    {{/tag}}
    {{#post}}
        {{#has tag="test"}}same content{{/has}}
    {{/post}}
{{/is}}

is there a smarter way to do this?

1 Like

Sorry but I’m a bit confused on your query and your code example. Are you trying to make a related posts area or something similar? Could you provide more context and info? Thanks!

I have a sidebar which is shown on all pages (tag, post, page etc). Now I would like to show an element in that sidebar if the tag == something. As example I have a post with tag top news. Now I would like to display the element in the sidebar if the user is on page tag/top-news or if the user is on a post having the tag top-news

Thanks for providing more detail. I think you’re looking for the #has helper, which can check if a post has a certain tag:

Note that the use of this helper will need to be within the #post context so the tags related to the post can be read by the #has helper.

Right, but what if I also would like to show my partial code on tag pages?

Thats why I provided this snippet to make it clear:

{{#is "tag, post"}}
    {{#tag}}
        {{#has slug="test"}}same content{{/has}}
    {{/tag}}
    {{#post}}
        {{#has tag="test"}}same content{{/has}}
    {{/post}}
{{/is}}

so on tag page with tag foo and on post with tag foo I have to show the same thing in the sidebar (which is shown on all pages)

Ah I see yes. In that case maybe blocks and partials would be useful to use here. blocks let you define and set areas of the template outside the context. Within your sidebar area you can set the following:

{{{block "sidebar-area"}}}

Then within your tag.hbs template:

{{#tag}}
  {{#has slug="test"}}
    {{#contentFor "sidebar-area"}}{{> sidebar-partial}}{{/contentFor}}
  {{/has}}
{{/tag}}

Finally, in your post.hbs template:

{{#post}}
  {{#has tag="test"}}
    {{#contentFor "sidebar-area"}}{{> sidebar-partial}}{{/contentFor}}
  {{/has}}
{{/post}}

The code within your partials/sidebar-partial.hbs would then be shown on the “test” tag page and any post with a tag of “test”. Does that help? Here’s some resource for reference: