【问题标题】:Jekyll forloop.last --> before last?Jekyll forloop.last --> 在最后一个之前?
【发布时间】:2017-02-05 02:44:02
【问题描述】:

我使用以下解决方案在 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. 如果当前帖子的作者与文章的作者相同 迭代帖子,但是标题不一样,那就填 神社绑定。

问题在于 {% if forloop.last == false %}&lt;hr&gt;{% endif %} 部分,因为如果forloop中有更多可迭代(post),它将显示 &lt;hr&gt; 标记,即使它是向用户显示的最后一个元素

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

【问题讨论】:

  • 您是否考虑过针对 forloop 长度减一来测试 forloop.index?
  • 这可能是一个解决方案,@JoostS,我会尝试和他们一起玩!
  • @JoostS 这不会真正起作用,因为您不知道循环的哪个索引将是执行if 语句的最后一次迭代。
  • 大卫在下面的回答让我意识到您的代码存在逻辑错误:请注意您将帖子限制为 4,然后循环浏览它们。如果您想查找的帖子都不在整个网站的前 4 个帖子中怎么办?
  • @KevinWorkman 你是对的,你们帮了我很多,它教会了我更多地思考我的情况并提出更一般的问题,而不是基于情况的问题。感谢您的宝贵时间!

标签: arrays if-statement for-loop jekyll liquid


【解决方案1】:

不会有一个简单的开箱即用的单线解决方案来解决这个问题。可以这样想:您实际上是在要求一个能够及时预测并确定这是否是if 语句最后一次评估为true 的功能。 Jekyll 很棒,但它无法预测未来!

您可以通过使用两个循环自己完成此操作:一个循环并计算您应该显示多少 &lt;hr&gt; 元素。然后另一个实际打印内容,检查您提出的计数以确定是否打印 &lt;hr&gt; 元素。

或者你可以使用 CSS 隐藏最后一个 &lt;hr&gt; 元素。 Google 是您的朋友。

【讨论】:

  • 感谢您的回答 Kevin,我现在只是在探索 Jekyll 的可能性,并在大约几周前使用它。看来你是对的,谢谢你的帮助。
  • @hzoltan 没问题。实际上我不久前才开始了解 Jekyll!
【解决方案2】:

打印一定数量的无打印条件的帖子

解决方案:使用循环limit

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

您将打印 4 个帖子,并且 forloop.last 始终有效。

在循环中打印一定数量的带有打印条件的帖子

解决方案:使用where 过滤器,counterbreak

现在您包含了条件打印:

  • 您不知道要打印哪些帖子以及要打印多少帖子。
  • 如果您没有打印上一篇文章,那么您的列表末尾会有一个 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 %}

【讨论】:

  • 我认为这不是 OP 的要求。 OP 专门询问最后一个 &lt;hr&gt; 元素。他们想在帖子之间打印&lt;hr&gt; 元素。换句话说,他们在询问如何在他们找到的 last 帖子之后打印&lt;hr&gt; 元素。这可能是 OP 将在其代码中面临的一个单独问题,但这不是他们要问的......但是。
  • @KevinWorkman 如果作者的帖子少于限制,我的代码就会出现问题。我已经更正了我的代码。它现在完全符合 OP 的要求。
猜你喜欢
  • 1970-01-01
  • 2015-07-16
  • 1970-01-01
  • 1970-01-01
  • 2012-03-02
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多