Jekyll forloop.last --> 倒数第二个?

3

我在Jekyll 3.2.1中创建了一个虚拟的“相关文章”模块,使用以下解决方案:

    {% for post in site.posts limit: 4 %}
    {% if page.author == post.author and page.title != post.title %}
    <div class="post-stream-item">
    <a href="{{ post.url | prepend: site.baseurl }}" title="{{ post.title  }}"><div class="post-stream-content">
        <img src="{{ post.thumbnail }}" width="80" height="auto" /><span class="post-stream-item-meta"><h3>{{ post.title }}</h3><p>{{ post.author }} on {{ post.date | date: "%b %-d, %Y" }} •     {% assign words = post.content | number_of_words %}
        {% if words <= 160 %}
        1 min
        {% else %}
        {{ words | plus: 159 | divided_by:160 }} mins
        {% endif %} read</p></span></div></a></div>{% if forloop.last == false %}<hr>{% endif %}

        {% endif %}
        {% endfor %}
  1. for循环遍历站点中的帖子列表并给出一个限制
  2. 如果当前帖子的作者与迭代帖子的作者相同,但标题不同,则填写jinja绑定。

问题出在 {% if forloop.last == false %}<hr>{% endif %} 这部分,因为如果for循环中有更多可迭代的(帖子),它将显示 <hr> 标签,即使它是最后一个展示给用户的元素

是否有任何属性可以引用列表中倒数第二个元素或任何更好的解决方案?


你有考虑过将 forloop.index 与 forloop 长度减一进行比较来测试吗? - Mr. Hugo
那可能是一个解决方案,@JoostS,我会尝试去尝试它们! - hzoltan
1
@JoostS 那样做不太可行,因为你事先不知道循环的哪个索引是最后一次执行 if 语句的迭代。 - Kevin Workman
David在下面的回答中让我意识到你的代码存在逻辑错误:请注意,您将帖子数量限制为4,然后通过它们循环。如果要查找的所有帖子都不在整个网站的前4个帖子中,该怎么办? - Kevin Workman
@KevinWorkman 你说得对,而且你们帮了我很多,这让我更多地思考我的情况并提出更一般性的问题,而不是基于具体情况的问题。感谢你们的时间! - hzoltan
@hzoltan,你能否更新你的问题并分享解决方案?我正在尝试实现两个循环,但是还无法完全搞定。 - ForTheWin
2个回答

3
这个问题并没有简单的开箱即用的解决方案。可以这样想:你基本上是在要求一种功能,它可以向前看,找出这是否是if语句最后一次评估为true。Jekyll很棒,但它无法预测未来!
你可以通过使用两个循环来自己实现:一个循环遍历并计算应显示多少个<hr>元素,另一个循环实际打印内容,根据你计算出的数量来决定是否打印<hr>元素。
或者,你可以只是使用CSS来隐藏最后一个<hr>元素。Google可以帮助你。

谢谢你的回答,Kevin。我现在正在探索Jekyll的可能性,并使用它已经有几周了。看起来你是对的,感谢你的帮助。 - hzoltan
@hzoltan 没问题。我自己也不久开始学习Jekyll! - Kevin Workman

2

打印一定数量的帖子,没有打印条件

解决方案:使用循环limit

{% for post in site.posts limit: 4 %}
    ... output code here
{% endfor %}

你将打印出精确的4篇文章,而且forloop.last总是有效的。
在循环中使用打印条件来打印一定数量的文章:
解决方法:使用“where”筛选器、一个计数器和“break”。
现在你可以包含一个条件打印:
- 你不知道会打印哪些文章以及数量。 - 如果你不打印最后一篇文章,你的列表结尾会有HR。
如果你想知道你可以打印多少篇文章,你可以使用{% assign authorsPosts = site.posts | where: "author", page.author %}和authorsPosts.size。
即使可用的文章数量少于你的限制,这段代码仍然能很好地完成它。
{% comment %} +++++ Max number of posts to print +++++ {% endcomment %}
{% assign limit = 4 %}

{% comment %} +++++ Select authors posts +++++{% endcomment %}
{% assign authorsPosts = site.posts | where: "author", page.author %}

{% comment %} +++++ If author's Posts number is less than limit, we change the limit +++++ {% endcomment %}
{% if limit >= authorsPosts.size %}
  {% comment %} +++++ Number of "listable" posts is author's posts number less 1 (the one actually printed) +++++ {% endcomment %}
  {% assign limit = authorsPosts.size | minus: 1 %}
{% endif %}

{% assign postsCounter = 0 %}
{% for post in authorsPosts %}
  {% if page.author == post.author and page.title != post.title %}

    {% assign postsCounter = postsCounter | plus: 1 %}

    <h3>{{ post.title }}</h3>

    {% comment %} +++++ Prints hr only if we are not printing the last post +++++ {% endcomment %}
    {% if postsCounter < limit %}<hr>{% endif %}

    {% comment %} +++++ Exit for loop if we reached the limit +++++ {% endcomment %}
    {% if postsCounter == limit %}{% break %}{% endif %}

  {% endif %}
{% endfor %}

我认为这不是提问者所询问的内容。提问者明确询问的是最后一个<hr>元素。他们想要在文章之间打印<hr>元素。换句话说,他们正在询问如何在找到最后一篇文章之后打印<hr>元素。这可能会是提问者在代码中面临的一个单独问题,但这不是他们目前所询问的内容。 - Kevin Workman
@KevinWorkman 我的代码存在一个问题,如果作者的帖子少于限制,则会出现问题。我已经纠正了我的代码。现在它完全符合OP的要求。 - David Jacquel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接