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

How to Overwrite (Nested) Jekyll Variables?

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

网站.components/button.html

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

data/sample.yml

#variables
button: Click Me

当我打开 url /button.html 时,变量运行良好:

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

问:有什么方法可以在页面中使用组件时覆盖变量?例如:

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

data/content.yml

#variables
button: Join Now

/样本-page.html

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

请注意组件不包括在内。

答案是否定的

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

唯一的方法是使用 include 并向它们传递变量。

_includes/button.html

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

_组件/button.html

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

sample-page.html

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