【问题标题】:Add an element on the last Jekyll loop of posts在帖子的最后一个 Jekyll 循环中添加一个元素
【发布时间】:2016-01-02 16:26:18
【问题描述】:

我确定这很简单,但找不到答案。

有一个标准的 Jekyll/Liquid 后迭代器。如何使用下面的{% if %} 语句将每个帖子的<hr> 元素除了放在最后?

<ul class="post-list">
    {% for post in site.posts %}
      {% if post.url %}
      <br>
        <li>
          <h2>
            <a class="post-link" href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a>
          </h2>
          <span class="post-meta">{{ post.date | date: "%b %-d, %Y" }}</span>
        </li>

        **** {% if post != $last %} ***** How do i do this??
          <hr>
        {% endif %}
      {% endif %}
    {% endfor %}
  </ul>

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    是的,有一个简单的解决方案。

    Liquid 有forloop object,可以在循环内使用它来访问它的一些属性。
    其中一个属性是forloop.last

    如果是 for 循环的最后一次迭代,则返回 true。如果不是最后一次迭代,则返回 false

    换句话说,你可以这样做:

    {% if forloop.last == false %}
      <hr>
    {% endif %}
    

    【讨论】:

      猜你喜欢
      • 2012-09-29
      • 2016-02-22
      • 1970-01-01
      • 2015-04-18
      • 2015-07-24
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多