gpt4 book ai didi

jekyll - 如何检查列表中是否存在值?

转载 作者:行者123 更新时间:2023-12-02 17:19:13 26 4
gpt4 key购买 nike

我想获取前言中有特定标签的帖子列表 (*)

我尝试了下面的代码,它遍历了当前帖子页面的所有标签(当前标签是t),然后遍历了所有帖子(p ) 检查他们是否有这个标签(出于调试原因只输出标题):

{% for t in page.tags %}
{% for p in site.posts %}
{% if t in p.tags %}
{{ p.title }}
{% endif %}
{% endfor %}
{% endfor %}

{% if t in p.tags %} 似乎失败了(我来自 Python 背景,所以我试了一下)而且我找不到 in 操作符在液体中。它退出了吗?

(*) 我提到了我想要实现的目标,以防有更直接的方法可以做到这一点,但我仍然对一般问题感兴趣。

最佳答案

按照您的示例,可以使用 contains 来完成标签:

contains can also check for the presence of a string in an array of strings.

{% if product.tags contains "outdoor" %}   
This product is great for
using outdoors!
{% endif %}

因此,要获取前言中具有特定标签的帖子列表(在本例中为带有标签 mytag 的帖子):

{% assign posts_with_mytag =  site.posts | where_exp:"item",
"item.tags contains 'mytag'" %}

{% for post in posts_with_mytag %}
<a href="{{post.url}}">{{post.title}}</a>
{% endfor %}

关于jekyll - 如何检查列表中是否存在值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44140253/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com