如何覆盖(嵌套)Jekyll变量?

时间:2016-05-31 12:14:59

标签: variables jekyll liquid

我的网站中有一个组件的集合,它们填充了由.yml文件中的变量指定的内容。

site.components / button.html

---
title: Button
---
{% assign yml = 'sample' %}
<a href="#">{{ site.data.[yml].button }}</a>

数据/ sample.yml

#variables
button: Click Me

当我打开网址 /button.html 时,该变量效果很好:

#Page Output
<html>
  <a href="#">Click Me</a>
</html>

问:在页面中使用组件时,有没有办法覆盖变量?例如:

---
title: A Sample Page 
---
{% assign yml = 'content'%}
{{ site.components | where:"title" : "Button" }}

数据/ content.yml

#variables
button: Join Now

/sample-page.html

#Page Output
<html>
  <a href="#">Join Now</a>
</html>

请注意,组件不包括在内。

1 个答案:

答案 0 :(得分:2)

答案是否定的。

当您执行{{ site.components | where:"title" : "Button" }}时,您会得到一系列与标题匹配的Jekyll文档。这些文件已经过液体分析。你不能指示jekyll再次解析它们。

唯一的方法是使用包含并传递变量。

_includes / button.html

<a href="#">{{ site.data.[include.text].button }}</a>

_components / button.html

---
title: Button
---
{% include button.html text='sample' %}

样品page.html中

---
title: A Sample Page
---
{% include button.html text='content' %}