【问题标题】:Jekyll place variable inside logic expressionJekyll 将变量放在逻辑表达式中
【发布时间】:2014-05-25 02:49:54
【问题描述】:

我正在尝试执行以下操作:

{% for post in site.categories.{{ post.designer }} %}

因此,当将上述代码放在单个帖子中时,它可以显示当前帖子类别中的帖子列表。

但是我认为它不起作用,因为它只是不断返回未定义。我的问题是,在 Jekyll 或 Liquid 中是否可以在逻辑表达式中放置变量?

谢谢

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    我想“设计师”是您帖子的类别?
    如果是,则无法通过post.designer 获取。

    您需要改用page.categories(根据Page variables)。

    一篇文章可以有多个类别,因此您不能只将 page.categories 放在循环中,因为它是一个数组。

    有两种可能的解决方案:

    1. 遍历帖子的所有类别,然后为每个类别循环:

      {% for cat in page.categories %}
        <h1>{{ cat }}</h1>
        <ul>
          {% for post in site.categories[cat] %}
            <li><a href="{{ post.url }}">{{ post.title }}</a></li>
          {% endfor %}
        </ul>
      {% endfor %}
      
    2. 如果您的帖子只有一个类别,您可以在我的第一个示例中省略外部循环,只使用the first element of the page.categories array

      <ul>
        {% for post in site.categories[page.categories.first] %}
          <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
      </ul>
      

      {% assign firstcat = page.categories | first %}
      
      <ul>
        {% for post in site.categories[firstcat] %}
          <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
      </ul>
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多