如何将变量传递给 jekyll 布局?
How can I pass a variable to a jekyll layout?
我不明白如何将变量传递给 Jekyll 布局。我有一个降价文件 header:
---
layout: post
next_tutorial: next_tutorial_name
---
然后,在 post 布局中,我有这个:
---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>
{{ content }}
{% if site.next_tutorial %}
<h2>{{ site.next_tutorial }}</h2>
{% endif %}
</article>
但是 h2 元素从未出现。我尝试删除 if,但结果是一样的。我做错了什么?
你所说的 "header 降价文件" 实际上在 Jekyll 中有一个名字,叫做 Front Matter。
前面的元素可以通过 page
变量访问,如 here and there 所指。
因此,您的布局应为:
---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>
{{ content }}
{% if page.next_tutorial %}
<h2>{{ page.next_tutorial }}</h2>
{% endif %}
</article>
我不明白如何将变量传递给 Jekyll 布局。我有一个降价文件 header:
---
layout: post
next_tutorial: next_tutorial_name
---
然后,在 post 布局中,我有这个:
---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>
{{ content }}
{% if site.next_tutorial %}
<h2>{{ site.next_tutorial }}</h2>
{% endif %}
</article>
但是 h2 元素从未出现。我尝试删除 if,但结果是一样的。我做错了什么?
你所说的 "header 降价文件" 实际上在 Jekyll 中有一个名字,叫做 Front Matter。
前面的元素可以通过 page
变量访问,如 here and there 所指。
因此,您的布局应为:
---
layout: default
---
<article id="postBody">
<h1>{{ page.title }}</h1>
<p>{{ page.date | date_to_string }}</p>
{{ content }}
{% if page.next_tutorial %}
<h2>{{ page.next_tutorial }}</h2>
{% endif %}
</article>