Search code examples
jekyllliquid

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


Solution

  • 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 %}