Jekyll forloop.last --> 最后一个?
Jekyll forloop.last --> before last?
我在 Jekyll 3.2.1 中创建了一个虚拟 "related posts",解决方案如下:
{% 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 %}
- for 循环遍历站点中的 posts 列表并给出
这是一个极限
- 如果当前post的作者与下一个的作者相同
迭代了post,但是标题不一样,那就补上
jinja 绑定。
问题出在 {% if forloop.last == false %}<hr>{% endif %}
部分,因为如果forloop中有更多的迭代(post),它将显示 <hr>
标签,即使它是向用户显示的最后一个元素.
是否有任何属性可以引用列表的倒数第二个元素或对此有更好的解决方案?
不会有一个简单的开箱即用的单线解决方案。以这种方式思考:您本质上是在请求一个及时向前看的功能,并确定这是否是 if
语句最后一次计算为 true
。 Jekyll是伟大的,但它不能预知未来!
您可以使用两个循环自己完成此操作:一个循环遍历并计算您应该显示多少 <hr>
个元素。然后另一个实际打印出来的东西,检查你想出的计数来决定是否打印 <hr>
元素。
或者您可以只使用 CSS 隐藏最后一个 <hr>
元素。 Google 是你的朋友。
打印一定数量的post无打印条件
解决方案:使用循环 limit
{% for post in site.posts limit: 4 %}
... output code here
{% endfor %}
您将恰好打印 4 post 秒并且 forloop.last 始终有效。
循环打印一定数量的post有打印条件
解决方案:使用 where
过滤器,一个 counter
和 break
既然您包含了条件打印:
- 您不知道将打印哪些和多少 post。
- 如果你不打印 last post,你的列表末尾有一个 HR。
如果您想知道可以打印多少post,可以使用{% assign authorsPosts = site.posts | where: "author", page.author %}
和authorsPosts.size
。
即使可用 post 的数量少于您的限制,此代码也能很好地完成。
{% 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 %}
我在 Jekyll 3.2.1 中创建了一个虚拟 "related posts",解决方案如下:
{% 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 %}
- for 循环遍历站点中的 posts 列表并给出 这是一个极限
- 如果当前post的作者与下一个的作者相同 迭代了post,但是标题不一样,那就补上 jinja 绑定。
问题出在 {% if forloop.last == false %}<hr>{% endif %}
部分,因为如果forloop中有更多的迭代(post),它将显示 <hr>
标签,即使它是向用户显示的最后一个元素.
是否有任何属性可以引用列表的倒数第二个元素或对此有更好的解决方案?
不会有一个简单的开箱即用的单线解决方案。以这种方式思考:您本质上是在请求一个及时向前看的功能,并确定这是否是 if
语句最后一次计算为 true
。 Jekyll是伟大的,但它不能预知未来!
您可以使用两个循环自己完成此操作:一个循环遍历并计算您应该显示多少 <hr>
个元素。然后另一个实际打印出来的东西,检查你想出的计数来决定是否打印 <hr>
元素。
或者您可以只使用 CSS 隐藏最后一个 <hr>
元素。 Google 是你的朋友。
打印一定数量的post无打印条件
解决方案:使用循环 limit
{% for post in site.posts limit: 4 %}
... output code here
{% endfor %}
您将恰好打印 4 post 秒并且 forloop.last 始终有效。
循环打印一定数量的post有打印条件
解决方案:使用 where
过滤器,一个 counter
和 break
既然您包含了条件打印:
- 您不知道将打印哪些和多少 post。
- 如果你不打印 last post,你的列表末尾有一个 HR。
如果您想知道可以打印多少post,可以使用{% assign authorsPosts = site.posts | where: "author", page.author %}
和authorsPosts.size
。
即使可用 post 的数量少于您的限制,此代码也能很好地完成。
{% 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 %}