Check if a tag is in a list?

Hi folks,

I’m stumped on how to pull this off with handlebars. I’d appreciate it if someone could give me a nudge in the right direction.

In my tag.hbs page, I’d like to check if the tag is in a list. I have that list as a @custom.mytagslist list (comma separated), which I use as a parameter to filter elsewhere on the site.

I tried the #has helper, but I can’t figure out how to get it to work with a list of tags in a tag (not post) context.

I guess I could {{#get tags filter=“slug:[{{@custom.mytagslist}}]+slug:slug”}} and then check for zero or one tag coming back, but that feels like the wrong way to do it.

So… is there a right way?

Thanks!

The #match helper comes to my mind, but the problem will be how to get the current tag name instead of the static one. I thought to share it anyway; maybe there will be a workaround.

{{#get 'tags' filter='slug:[{{@custom.mytagslist}}]'}}
  {{#foreach tags}}
    {{#match name "CURRENT_TAG_NAME"}}
    {{/match}}
  {{/foreach}}
{{/get}}
1 Like

Yes, the best I’ve come up with is to run an extra {{#get ‘tags’ filter=‘slug:[{{@custom.mytagslist}}]+slug:{{slug}}’}} and then to check if if I get a tag (using #match with tags.length - that avoids looping over the results and doing repeated matches - although I’m not certain what’s more performant). But that seems inefficient - it means I’m asking the database to do the work of deciding if the two slugs match, when both are in the current context already.