Search code examples
jekyllgithub-pagesliquid

Using variables in conditional expressions in if-statements in Jekyll (with Liquid)


I am trying to make a website using Jekyll as a local test for github-pages. I wrote like the following within my _layouts/default.html, but it seemed that ‘{{ category.name }}’ in the if statement was not recognized as a variable. Can’t we use variables in condition in Jekyll?

{% for category in site.categories %}
    {% if page.dir contains ‘{{ category.name }}’ %}
        <a href="{{ category.url }}">
        {{ category.name }}
        </a>
    {% endif %}
{% endfor %}

I referred to these manual:


Solution

  • You do not need to put a variable inside double curly braces when evaluating a expression in tags. You simply need to do:

    {% if page.dir contains category.name %}