对于 Liquid,您可以在 include 中传递一个变量
{%- assign global_var = "VALUE" -%}
{%- include 'YOUR_FILE' global_var: global_var -%}
Run Code Online (Sandbox Code Playgroud)
对于 Shopify Liquid,您可以执行以下操作:
有一个解决方法,您可以将主题设置中的全局变量设置为一个选项 config/settings_schema.json
{
"type": "text",
"id": "global_variable",
"label": "global variable",
"default": "Variable value"
},
Run Code Online (Sandbox Code Playgroud)
你可以通过 Liquid 文件访问它
settings.global_variable
Run Code Online (Sandbox Code Playgroud)
但该值取决于您在主题设置中输入的内容。
如果您需要更动态的方式,您可以通过 ajax 设置购物车属性,例如:
$.ajax({
type: 'POST',
url: '/cart/update.js',
data: { attributes: {'global_variable': "MY_VALUE"} },
dataType: 'json',
success: function(cart) {
location.reload();
}
});
Run Code Online (Sandbox Code Playgroud)
然后通过cart.attributes.global_variable在主题中的任何位置访问它但是每次购物车为空时您都必须更新它
只要您使用
{% assign variable = value %}
Run Code Online (Sandbox Code Playgroud)
您应该能够在文件中的任何位置以及分配后包含的任何文件中获取该值。
我相信这是最接近全局变量的。
| 归档时间: |
|
| 查看次数: |
4309 次 |
| 最近记录: |