使用!=逻辑过滤Liquid / Jekyll数组?

时间:2015-12-07 06:56:40

标签: jekyll liquid

我正在使用Jekyll构建一个网站,并且正在努力创建一个“最近发布的帖子”。使用Liquid逻辑在每个帖子的末尾添加数组。

我希望此数组包含所有帖子,但您当前所在页面的帖子除外。

所以,我开始:

{% for post in site.posts limit:2 %}
  {% if post.title != page.title %}
    //render the post
  {% endif %}
{% endfor %}

除了我的limit: 2导致问题外,这是有效的。由于Liquid限制之前 if逻辑,如果它确实遇到标题等于当前页面标题的帖子,它将(正确地)不呈现它,但它会考虑限制"满意" - 而我最终只有1个相关的帖子而不是2个。

接下来我尝试创建自己的帖子数组:

{% assign currentPostTitle = "{{ page.title }}" %}
{% assign allPostsButThisOne = (site.posts | where: "title" != currentPostTitle) %}

{% for post in allPostsButThisOne limit:2 %}
  //render the post          
{% endfor %}

这不起作用,因为我无法使where过滤器接受!=逻辑。

如何成功解决此问题?

2 个答案:

答案 0 :(得分:3)

您可以使用计数器:

{% assign maxCount = 2 %}
{% assign count = 0 %}
<ul>
{% for post in site.posts %}
  {% if post.title != page.title and count < maxCount %}
    {% assign count = count | plus: 1 %}
    <li>{{ post.title }}</li>
  {% endif %}
{% endfor %}
</ul>

答案 1 :(得分:0)

正如您所提到的,您只会错过这段代码:

{% assign allPostsButThisOne = (site.posts | where: "title" != currentPostTitle) %}

我尝试将containsunless

一起使用
{% assign allPostsButThisOne = (site.posts | where_exp:"post", "unless post.title contains currentPostTitle") %}