遍历文档属性
Loop over document properties
在我的 markdown header 我添加了一个自定义行:
---
layout: docs
title: "My title"
date: 2015-09-18 22:40:58
permalink: /some/url/
custom: valueA valueB <---
---
并且我设法编写了以下处理这些值的模板:
{% capture custo %}{{page.custom}}{% endcapture %}
{% assign cust = custo|split: %}
{% for cus in cust%}
<code>{{ cus }}</code>
{% endfor %}
但是,这对我来说似乎太复杂了。我试过了
将赋值直接移动到 for
中,它编译但仅将所有内容输出为一个值,而不是单独的值
{% for cus in custo|split: %}
摆脱 capture
,但我得到 undefined method 'split' for nil:NilClass
做
{% assign cust = page.custom|split: %}
我的模板可以简化吗?还是必须这样?或者它甚至是错误的方法?
对此进行更多研究后,我发现 header 不仅是 header,而且实际上是 YML。因此我可以做到
---
layout: docs
title: "My title"
date: 2015-09-18 22:40:58
permalink: /some/url/
custom:
1: valueA
2: valueB
---
然后使用普通循环
{% for cus in page.custom %}
<code>{{ cus[1] }}</code>
{% endfor %}
在我的 markdown header 我添加了一个自定义行:
---
layout: docs
title: "My title"
date: 2015-09-18 22:40:58
permalink: /some/url/
custom: valueA valueB <---
---
并且我设法编写了以下处理这些值的模板:
{% capture custo %}{{page.custom}}{% endcapture %}
{% assign cust = custo|split: %}
{% for cus in cust%}
<code>{{ cus }}</code>
{% endfor %}
但是,这对我来说似乎太复杂了。我试过了
将赋值直接移动到
for
中,它编译但仅将所有内容输出为一个值,而不是单独的值{% for cus in custo|split: %}
摆脱
capture
,但我得到undefined method 'split' for nil:NilClass
做{% assign cust = page.custom|split: %}
我的模板可以简化吗?还是必须这样?或者它甚至是错误的方法?
对此进行更多研究后,我发现 header 不仅是 header,而且实际上是 YML。因此我可以做到
---
layout: docs
title: "My title"
date: 2015-09-18 22:40:58
permalink: /some/url/
custom:
1: valueA
2: valueB
---
然后使用普通循环
{% for cus in page.custom %}
<code>{{ cus[1] }}</code>
{% endfor %}