我可以从 Jekyll 包含中修改全局 Liquid 变量吗?
Can I modify a global Liquid variable from inside a Jekyll include?
我想创建单独的 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 传递?
这可以在调用 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,
更新
它也可以在不将 "global" 变量作为参数传递的情况下工作,唯一的要求是在调用 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 %}
我想创建单独的 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 传递?
这可以在调用 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,
更新
它也可以在不将 "global" 变量作为参数传递的情况下工作,唯一的要求是在调用 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 %}