我想在我的Jekyll帖子的首页定义一个页面的部分,像这样:
---
title: Foobar
sections:
- First
- Second
- Third
---
这工作得很好,但我现在需要有一些嵌套项的部分;无论我如何尝试,Jekyll要么对任何嵌套对象不满意,要么输出嵌套的"文本",例如:
---
title: Foobar
sections:
- First
- Second:
- Nested one
- Nested two
- Third
---
…将解释"-第二个:-嵌套的一个-嵌套的两个"作为一个列表项。
我使用这些部分在页面主导航中生成内联锚链接。我如何在frontmatter中为此设置一个嵌套列表?
不能100%确定你尝试过什么,但Jekyll有时会很棘手。前页是一个数组,嵌套的列表是一个散列。为了使结果可见,我使用{{ variable | inspect }}
.
显示整个正面内容:
代码:{{ page.sections | inspect }}
结果:["First", {"Second"=>["Nested one", "Nested two"]}, "Third"]
访问首索引(嵌套部分):
代码:{{ page.sections[1] | inspect }}
结果:{"Second"=>["Nested one", "Nested two"]}
访问值的键和索引:
代码:{{ page.sections[1]["Second"][0] | inspect }}
结果:Nested one
你也可以使用循环:
{% for item in page.sections %}
{% if item["Second"] %}
{{ item["Second"][0] }}
{{ item["Second"][1] }}
{% else %}
{{ item }}
{% endif %}
{% endfor %}