Jekyll在索引处获得标签

时间:2018-09-27 17:08:03

标签: indexing tags jekyll

我正在自学Jekyll和Liquid,想知道如何获取数组的索引项?

我可以创建一个page.tags数组并遍历它们:

{% assign tags = pages.tags %}
{% for tag in tags %}
{% endfor %}

但是说有四个标签,我想访问索引2的标签。我已经看到了一些类似这样的代码:

{% for i in 1...page.tags %}

{% endfor %}

但是我似乎无法使索引正常工作,但这些失败了:

{% for i in 1...page.tags %}
    <p>{{page.tags[i]}}</p>
{% endfor %}

{% for i in 1...page.tags %}
    <p>{{i}}</p>
{% endfor %}

2 个答案:

答案 0 :(得分:2)

我对您的代码做了一些改进:

<div id="topNav">
  <ul>
    {% for tag in page.tags %}
      {% if forloop.first %}         
        <li class="fadeIn firstItem notLogo">{{tag}}</li>
      {% else %}
        <li class="fadeIn notLogo">{{tag}}</li>
      {% endif %}
    {% endfor %}
  </ul>
</div>

来源:https://help.shopify.com/en/themes/liquid/objects/for-loops

答案 1 :(得分:0)

我用一种技巧解决了它。

<div id="topNav">

    <ul>

        {% assign count = 0 %}

        {% for tag in page.tags %}
            {% if count == 0 %}         

                <li class="fadeIn firstItem notLogo">{{tag}}</li>

            {% else %}

                <li class="fadeIn notLogo">{{tag}}</li>

            {% endif %}
            {% assign count = count | plus: 1 %}

        {% endfor %}

    </ul>


</div>