【问题标题】:How to Overwrite (Nested) Jekyll Variables?如何覆盖(嵌套)Jekyll 变量?
【发布时间】:2016-05-31 12:14:58
【问题描述】:

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

site.components/button.html

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

data/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" }}

data/content.yml

#variables
button: Join Now

/sample-page.html

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

注意组件不包括在内。

【问题讨论】:

    标签: variables jekyll liquid


    【解决方案1】:

    答案是否定的。

    当您执行{{ 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' %}
    

    sample-page.html

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

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 1970-01-01
      • 2016-11-16
      • 2018-05-13
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      • 1970-01-01
      相关资源
      最近更新 更多