Doug
Doug

Reputation: 35136

How do you check if a tag is present on a page?

Currently I'm doing this:

<div id="nav">
    <ul>
      {% for page in site.articles %}
         {% for tag in page.tags %}
            {% if tag == 'index' %}
              <li><a href="{{ page.url }}">{{page.title}}</a>
            {% endif %}
         {% endfor %}
      {% endfor %}
    <ul>
</div>

Seems bad. I see some people online saying you can do this:

{% if page.tags[index] %}
   ...
{% endif %}

...but this doesn't seem to work.

Is there some variation on this syntax that does actually work?

NB. jekyll --version -> jekyll 2.5.3

Upvotes: 3

Views: 898

Answers (1)

David Jacquel
David Jacquel

Reputation: 52809

There's no site.articles array, it's site.pages or site.posts.

You can do :

{% for p in site.pages %}
  {% if p.tags contains 'index' %}
     <li><a href="{{ p.url }}">{{p.title}}</a>
  {% endif %}
{% endfor %}

Note : do not use page variable in a loop because it is already used by the current page.

Upvotes: 3

Related Questions