【问题标题】:How to select specific items in Liquid如何在 Liquid 中选择特定项目
【发布时间】:2017-02-16 23:04:18
【问题描述】:

假设我有一个标签列表:iFix_6.3iFix_7.0iFix_7.1iFix_8.0announcement 等等......我只想在某些 上运行操作几个标签。如何检查这些多个值?

contains,但我正在寻找它的反面和多个值...

这是一个示例,我实际上过滤掉了所有包含iFix_6.3 标签的帖子,从而显示所有其他帖子。这实际上还行不通...另外需要扩展以适用于多个标签。

// posts with iFix_xxx tag should be filtered from the main posts view.
{% assign postUpdates = site.posts | where_exp:"item", "item.tags != 'iFix_6.3'" %} 
{% for post in postUpdates limit:10 %}
<div class="postItem inline">
    <p class="postDate">{% if post.pinned %}<span class="glyphicon glyphicon-pushpin"></span>{% endif %}{{post.date | date: '%B %d, %Y'}}</p>
    <p class="postTitle"><a href="{{site.baseurl}}{{post.url}}">{{post.title}}</a></p>
</div>
{% endfor %}

【问题讨论】:

  • 如何遍历所有帖子并使用 if 语句进行过滤?

标签: jekyll liquid


【解决方案1】:

您可以通过使用split 和逗号分隔标签的字符串构建一个排除标签的数组 (excluded_tags) 来做到这一点。然后,对于每篇文章,您都在文章的标签上进行迭代。使用contains 检查标签是否在excluded_tags 中,如果是则使用unless 控制流标签提高标志filtered_out 以不显示帖子。

{% assign excluded_tags = "iFix_6.2,iFix_6.3,announcement" | split : "," %}

{% for post in site.posts limit:10 %}
   {% assign filtered_out = False %}
   {% for tag in post.tags %}
       {% if excluded_tags contains tag %}
           {% assign filtered_out = True %}
           {% break %}
       {% endif %}
   {% endfor %}

   {% unless filtered_out %}
       ...
   {% endunless %}
{% endfor %}

【讨论】:

  • 可能有别的东西干扰了这里,因为应该排除的博客文章仍然出现在列表中。有什么方法可以调试过滤的内容和不过滤的内容?
  • 打印出{% for tag in post.tags %}循环中发生的事情以进行调试。
  • 对 n00bism 感到抱歉,但是,我该怎么做呢?
  • {{tag}}、{{excluded_tags}}、{{filtered_out}} 等
【解决方案2】:

看来unless 会解决这个问题。

{% assign postUpdates = site.posts | where_exp:"item", "unless item.tags contains 'iFix_6.3'" %} 

【讨论】:

    猜你喜欢
    • 2015-02-11
    • 2014-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    相关资源
    最近更新 更多