从液体中获取数组中的下一个和上一个元素 [英] Get next and previous elements out of an array in liquid

查看:200
本文介绍了从液体中获取数组中的下一个和上一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:

我想在液体模板中的数字上加1,然后将结果用作数组索引.

I want to add 1 to a number in a liquid template and use the result as an array index.

{% 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>

结果:

-Value of plus_one: 1
-This works: The Zone
-This doesn't:


长版:


Long version:

我正在使用Jekyll,没有任何插件.我想给当前帖子一个链接,指向下一个属于同一类别的帖子. (该类别在此代码中被硬编码为新闻".)

I'm using Jekyll, with no plugins. I want to give the current post a link to the next post that is in the same category. (The category is hardcoded to 'journal' in this code.)

我的代码遍历category数组中的所有帖子,寻找当前帖子.找到后,我尝试获取类别数组中的下一篇文章.

My code loops over all posts in the category array, looking for the current post. When it is found, I try to grab the next post in the category array.

{% 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>

结果:

<div>value of plus_one: 1</div>
<div>This doesn't work: </div>
<div>This does: A Blog Post Title</div>

我猜我的变量'plus_one'的值被视为字符串而不是数字.

I guess the value of my variable 'plus_one' is being treated as a string instead of a number.

有什么办法可以将其转换为数字?

Is there any way to convert it to a number?

还是有另一种方法可以实现我想要做的事情?

Or is there another way to achieve what I'm trying to do?

推荐答案

{% 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 }}">&larr;{{ next.title }}</a>
            </li>
        {% endunless %}
        {% unless forloop.first %}
            <li class="next">
            <a href="{{ site.baseurl }}{{ prev.url }}">{{ prev.title }}&rarr;</a>
            </li>
        {% endunless %}
    {% endif %}
    {% assign prev = post %}
{% endfor %}

正如您所提到的,您可以保存并使用上一个迭代值作为上一个帖子链接(在我的情况下,因为我不希望默认的最新顺序,所以我将其用作下一个帖子链接).对于下一个数组元素,可以使用forloop.index.这是for循环的从1开始的索引,将为您提供从0开始的数组的下一项.

As you have mentioned you can save and use the previous iteration value for the previous post link( in my case I use it as the next post link since I don't want the default newest-first order ). For the next array element you can use forloop.index. This is the 1-based index of the for loop and will give you the next item of a zero-based array.

这篇关于从液体中获取数组中的下一个和上一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆