2016-05-31 68 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' %}