【问题标题】:Jekyll get tag at indexJekyll 在索引处获取标签
【发布时间】:2018-09-27 17:08:03
【问题描述】:

我正在自学 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 %}

【问题讨论】:

    标签: indexing tags jekyll


    【解决方案1】:

    我稍微改进了你的代码:

    <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

    【讨论】:

    • 哈,谢谢 JoostS 我昨天才发现上面有那些高阶函数的页面。
    【解决方案2】:

    我已经用一种 hack 解决了这个问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      相关资源
      最近更新 更多