如何在Liquid中定义全局变量? [英] How to define global variables in Liquid?
问题描述
现在看来,我创建的变量无法跨文件通信.
Right now it seems variables I create can't communicate across files.
推荐答案
对于Liquid,您可以在包含项中传递变量
For Liquid you can pass a variable in the include
{%- assign global_var = "VALUE" -%}
{%- include 'YOUR_FILE' global_var: global_var -%}
对于Shopify液体,您可以执行以下操作:
For Shopify liquid you can do the following:
有一个解决方法,您可以在主题设置中将全局变量设置为选项 config/settings_schema.json
There is a work around this, you can set the global variable in the theme settings as an option config/settings_schema.json
{
"type": "text",
"id": "global_variable",
"label": "global variable",
"default": "Variable value"
},
,您可以通过以下方式在临时文件中访问它:
and you can access it in the liquid files through
settings.global_variable
但是该值取决于您在主题设置中输入的内容.
But the value is depending on what you enter in the theme settings.
如果您需要更多动态方式,则可以通过ajax设置购物车属性,例如:
If you need more dynamic way, you can set cart attributes through ajax like:
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { attributes: {'global_variable': "MY_VALUE"} },
dataType: 'json',
success: function(cart) {
location.reload();
}
});
然后通过以下方式在主题中的任何位置进行访问cart.attributes.global_variable但是每次购物车都是空的时候你必须更新它
And then access it any where in the theme through cart.attributes.global_variable But you have to update it each time the cart is empty
这篇关于如何在Liquid中定义全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!