Stefany Newman
Stefany Newman

Reputation: 524

Liquid conditional rendering

I have blog posts to which I add tags in the YFM like this:

tags: amp

I want Liquid to check whether the tag == amp and if so, to add a link in the blog-post.html layout file. I tried the code below:

{% if page.tags == "amp" %}
   <a href="#">link</a>
{% endif %}

But nothing gets outputted

Upvotes: 0

Views: 1020

Answers (1)

DC.Azndj
DC.Azndj

Reputation: 1396

The tags attribute in the YFM should actually be stored as an array since there can be multiple tags, as seen in the docs.

tags: [amp, foo, bar]

When checking the tags, use the contains liquid filter.

{% if page.tags contains 'amp' %}
   <a href="#">link</a>
{% endif %}

Upvotes: 4

Related Questions