如何检查页面上是否存在标签?

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

目前我正在这样做:

<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>

看起来很糟糕。我看到网上有人说你可以这样做:

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

...但这似乎不起作用。

此语法是否有一些实际有效的变体?

注意。 jekyll --version -> jekyll 2.5.3

没有 site.articles 数组,它是 site.pagessite.posts

你可以做到:

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

注意 : 不要在循环中使用 page 变量,因为它已经被当前页面使用了。