2016-05-31 69 views
0

我在我的网站中收集了组件,它们使用.yml文件中的变量指定的内容填充。如何覆盖(嵌套)Jekyll变量?

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> 

注组件不包括。

回答

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' %}