Jekyll 在液体中从数组中取出下一个和上一个元素
简短版本: 我想给液体模板中的一个数字加1,并将结果用作数组索引Jekyll 在液体中从数组中取出下一个和上一个元素,jekyll,liquid,Jekyll,Liquid,简短版本: 我想给液体模板中的一个数字加1,并将结果用作数组索引 {% capture plus_one %}{{ 0 | plus: 1 }}{% endcapture %} <div>-Value of plus_one: {{plus_one}}</div> <div>-This works: {{site.posts[1].title}}</div> <div>-This doesn't: {{site.posts[plus_o
{% capture plus_one %}{{ 0 | plus: 1 }}{% endcapture %}
<div>-Value of plus_one: {{plus_one}}</div>
<div>-This works: {{site.posts[1].title}}</div>
<div>-This doesn't: {{site.posts[plus_one].title}}</div>
长版本: 我用的是Jekyll,没有插件。我想给当前的文章一个链接到下一篇属于同一类别的文章。(该类别在此代码中硬编码为“日记账”。) 我的代码在类别数组中的所有帖子上循环,查找当前帖子。当它被找到时,我试图抓住类别数组中的下一篇文章
{% for num in (0..site.categories.journal.size) %}
{% assign page2 = site.categories.journal[num] %}
{% if page2.title == page.title and page2.date == page.date %}
{% capture plus_one %}{{ num | plus: 1 }}{% endcapture %}
{% endif %}
{% endfor %}
<div>value of plus_one: {{plus_one}}</div>
<div>This doesn't work: {{site.categories.journal[plus_one].title}}</div>
<div>This does: {{site.categories.journal[1].title}}</div>
{(0..site.categories.journal.size)%%中num的%
{%assign page2=site.categories.journal[num]}
{%如果page2.title==page.title和page2.date==page.date%}
{%capture plus_one%}{{num | plus:1}{%endcapture%}
{%endif%}
{%endfor%}
plus_one的值:{{plus_one}
这不起作用:{{site.categories.journal[plus_one].title}
这是:{site.categories.journal[1].title}
结果:
-Value of plus_one: 1
-This works: The Zone
-This doesn't:
<div>value of plus_one: 1</div>
<div>This doesn't work: </div>
<div>This does: A Blog Post Title</div>
加号1的值:1
这不起作用:
这就是:一篇博客文章的标题
我猜变量“plus_one”的值被视为字符串而不是数字
有没有办法把它转换成一个数字
或者有没有其他方法可以实现我的目标?你可以通过
页面访问下一篇/上一篇文章。下一篇或页面。上一篇
当您希望它位于同一类别中时,必须将其分配给一个临时变量,并迭代该步骤,直到找到匹配的帖子(同一类别中的帖子)
由于液体的工作方式,您不能使用plus_one变量-它只是一个字符串。在liquid中使用数学非常复杂,当我在liquid中实现加权标记云时,我几乎头疼不已。{%for category in site.categories%}
{% for category in site.categories %}
{% assign catg_name = category.first %}
{% if catg_name == page.category %}
{% assign catg_posts = category.last %}
{% endif %}
{% endfor %}
{% for post in catg_posts %}
{% if post.title == page.title %}
{% unless forloop.last %}
{% assign next = catg_posts[forloop.index] %}
<li class="previous">
<a href="{{ site.baseurl }}{{ next.url }}">←{{ next.title }}</a>
</li>
{% endunless %}
{% unless forloop.first %}
<li class="next">
<a href="{{ site.baseurl }}{{ prev.url }}">{{ prev.title }}→</a>
</li>
{% endunless %}
{% endif %}
{% assign prev = post %}
{% endfor %}
{%assign catg_name=category.first%}
{%if catg_name==page.category%}
{%assign catg_posts=category.last%}
{%endif%}
{%endfor%}
{catg_posts%}
{%if post.title==page.title%}
{%forloop.last%}
{%assign next=catg_posts[forloop.index]}
{%end除非%}
{%forloop.first%}
{%end除非%}
{%endif%}
{%assign prev=post%}
{%endfor%}
正如您所提到的,您可以保存并使用上一个帖子链接的上一个迭代值(在我的例子中,我使用它作为下一个帖子链接,因为我不想要默认的最新的第一个订单)。对于下一个数组元素,可以使用forloop.index
。这是for循环的基于1的索引,将为您提供基于零的数组的下一项。assign
保留数字:
{% for item in items %}
{% assign next_i = forloop.index0 | plus: 1 %}
{% assign prev_i = forloop.index0 | minus: 1 %}
{{ items[next_i] }}
{{ items[prev_i] }}
{% endfor %}
我有一个类似的问题,使用上面的答案,我得到了以下工作在我的网站上。可能对其他人有帮助
{% for num in (0..site.categories.work.size) %}
{% assign page2 = site.categories.work[num] %}
{% if page2.title == page.title and page2.date == page.date %}
{% assign next_i = forloop.index0 | plus: 1 %}
{% assign prev_i = forloop.index0 | minus: 2 %}
{% if next_i == site.categories.work.size %}
{% assign next_i = 1 %}
{% endif %}
{% endif %}
{% endfor %}
<nav class="pagination">
<div class="next">
<a href="work/{{site.categories.work[next_i].slug}}/">Next</a>
</div>
<div class="previous">
<a href="work/{{site.categories.work[prev_i].slug}}/">Previous</a>
</div>
</nav>
{(0..site.categories.work.size)%%中num的%
{%assign page2=site.categories.work[num]}
{%如果page2.title==page.title和page2.date==page.date%}
{%assign next_i=forloop.index0 | plus:1%}
{%assign prev_i=forloop.index0 |减:2%}
{%if next_i==site.categories.work.size%}
{%assign next_i=1%}
{%endif%}
{%endif%}
{%endfor%}
我不知道jekyll在接受答案后是否有所改变,但我必须做一些改变才能让它正常工作。根据jekyll变量文档,没有page.category,只有page.categories的列表。在我的网站上,我每个帖子只使用一个类别,所以第一个类别对我来说很有用。如果你在每篇文章中使用多个类别,你必须对此进行调整。我也遇到了一些问题,因为我的类别是大写的,所以我添加了downcase。最后,在我的config.yml中,我使用的是url而不是baseurl。希望这能帮助其他人,因为这是我发现的最简单的方法,可以按类别将上一个/下一个链接添加到所有帖子中
{% for category in site.categories %}
{% assign catg_name = category.first %}
{% if catg_name == page.categories.first | downcase %}
{% assign catg_posts = category.last %}
{% endif %}
{% endfor %}
{% for post in catg_posts %}
{% if post.title == page.title %}
{% unless forloop.last %}
{% assign next = catg_posts[forloop.index] %}
<a href="{{ site.url }}{{ next.url }}">←{{ next.title }}</a> |
{% endunless %}
{% unless forloop.first %}
| <a href="{{ site.url }}{{ prev.url }}">{{ prev.title }}→</a>
{% endunless %}
{% endif %}
{% assign prev = post %}
{% endfor %}
{%用于site.categories%}
{%assign catg_name=category.first%}
{%if catg_name==page.categories.first | downcase%}
{%assign catg_posts=category.last%}
{%endif%}
{%endfor%}
{catg_posts%}
{%if post.title==page.title%}
{%forloop.last%}
{%assign next=catg_posts[forloop.index]}
|
{%end除非%}
{%forloop.first%}
|
{%end除非%}
{%endif%}
{%assign prev=post%}
{%endfor%}
这听起来比我在下面发布的解决方案好,但是你能举例说明如何“迭代该步骤”,以便我们可以继续调用page。接下来,直到我们找到一个具有匹配类别的页面?迭代是编程的基本概念,你甚至可以在自己发布的答案中这样做,所以我真的不知道该怎么回答。对不起,我的问题不是很清楚。我想问一下循环的条件。理想情况下,我们只希望循环,直到找到匹配的页面。但是,在到达最后一页(page.next为null)之前循环是可以接受的。到目前为止,我还没有找到一种方法让Liquid创建一个循环,该循环将在以下任一条件下终止。在jekyll中,您只能在[collection]
循环中为[item]编写。如何迭代升级?刚刚注意到我使用的“| downcase”参数为发布的新3.0.0版本的jekyll serve创建“无效管道”。如果删除它,它将再次工作。