我想创建单独的Jekyll包含,它们都可以引用相同的公共变量。这是一个简化的场景。
我使用以下Liquid代码创建_include/setter.html
:
{% globalString | append: include.add | append: "," %}
然后我使用以下Liquid代码创建_include/getter.html
:
we have {{ globalString }}
然后在我的页面中我说:
{% include setter.html add = "one" %}
{% include setter.html add = "two" %}
{% include getter.html %}
我希望看到类似we have one,two,
的结果。
但当然globalString
不存在,所以这不起作用。我似乎无法在site
或page
中创建可从包含中看到的新变量。现在我用capture
笨拙地解决这个问题。有没有更好的方法在Jekyll中传递包含 out 的数据?
答案 0 :(得分:3)
可以在调用 includes 之前设置全局变量并将其作为参数传递来完成:
_includes / setter.html :
before: {{include.globalString}}<br>
{% assign globalString = include.globalString | append: include.add | append: "," %}
after: {{globalString}}<br>
_includes / getter.html :we have {{ include.globalString }}
然后:
{% assign globalString = "" %}
{% include setter.html add = "one" globalString=globalString%}
{% include setter.html add = "two" globalString=globalString%}
{% include getter.html globalString=globalString%}
输出:
before:
after: one,
before: one,
after: one,two,
we have one,two,
它也可以在没有通过&#34;全球&#34;变量作为参数,唯一的要求是在调用 includes :
之前定义它<强> _includes / setter.html 强>:
before: {{globalString}}<br>
{% assign globalString = globalString | append: include.add | append: "," %}
after: {{globalString}}<br>
_includes / getter.html :we have {{ globalString }}
{% assign globalString = "" %}
{% include setter.html add = "one" %}
{% include setter.html add = "two" %}
{% include getter.html %}