【问题标题】:Liquid/Jekyll: How to check for no posts when one has two or more conditions?Liquid/Jekyll:当一个人有两个或多个条件时,如何检查没有帖子?
【发布时间】:2018-10-16 20:32:53
【问题描述】:

我无法理解如何为具有两个变量的特定条件语句显示“不存在帖子”消息。

在此示例中,假设我有一个“动物”集合 - 在特定页面上,我想要一个显示“食草动物的灵长类动物”的部分:

  {% for animal in site.animal %}
    {% if animal.species == "primate" and animal.type == "herbivore" %}

      {{ animal.content }}

    {% endif %}
  {% endfor %}

我想做的是这样的(伪代码):

{% if POSTS_EXIST_FOR_THIS_COMBO: (animal.species == "primate" and animal.type == "herbivore") %}
      {% for animal in site.animal %}
        {% if animal.species == "primate" and animal.type == "herbivore" %}
          {{ animal.content }}
        {% endif %}
      {% endfor %}
{% else %}
     There are no posts for this category.
{% endif %}

注意:这与this 之类的示例不同,因为我有两个参数要检查。有人可以提供有关语法的建议吗?

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    我认为您可以执行以下操作,首先从 site.animal 过滤所有 species=primate,然后从该池中过滤 type=herbivore,然后检查结果是否存在。

    {% assign animals = site.animal | where:"species","primate" | where:"type","herbivore" %}
    
    {% if animals %}
      {% for animal in animals %}
        {{ animal.content }}
      {% endfor %}
    {% endif %}
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多