【问题标题】:Jekyll for loop over frontmatter array returning twiceJekyll for 循环遍历 frontmatter 数组返回两次
【发布时间】:2019-12-10 09:29:42
【问题描述】:

使用 Jekyll 3.8.5(用于 Github 页面)。

我已经建立了一个集合_resources,其中包含这样的文件:

---
title: Resource 1
categories:
- title: cat3
  level: 1
- title: cat2
  level: 2
---

我正在使用一个简单的 Liquid for 循环遍历类别及其数据:

<ul>
        {% for category in resource.categories %}
                <li> Category: {{ category.title }} | Level: {{ category.level }}</li>
        {% endfor %}
    </ul>

预期的结果应该是:

<ul>
    <li> Category: cat3 | Level: 1</li>
    <li> Category: cat2 | Level: 2</li>
</ul>

相反,我得到了额外的&lt;li&gt; 元素:

<ul>
   <li> Category: cat3 | Level: 1</li>
   <li> Category: cat2 | Level: 2</li>
   <li> Category:  | Level: </li>
   <li> Category:  | Level: </li>
</ul>

我使用{{ categories.size }} 进行了检查,它返回了4

所以,我确定我的 YAML 格式有问题,让 Jekyll 认为有两个额外的类别。

我尝试删除 YAML 中的连字符,但 Jekyll 抛出错误:

没有将哈希隐式转换为数组

非常感谢您的任何帮助。

谢谢,

标记

【问题讨论】:

    标签: yaml jekyll liquid


    【解决方案1】:

    tagtagscategorycategories 对集合中的文档(包括帖子)具有特殊含义。

    Jekyll 期望文档的 tagcategory 属性是简单的字符串,并且 属性 tagscategories 是简单的数组。

    您可以尝试将 categories 键换成其他名称,例如 foobars.. 然后,如果您更新后的 Liquid 循环呈现预期的标记,则一切就绪。将foobars 替换为更合适的标签。

    如果问题仍然存在,则说明您的布局/包含有问题。

    【讨论】:

    • 谢谢!如此简单的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多