简体   繁体   English

使用带有 Shopify 液体标签的 js 变量

[英]Use js variable with Shopify liquid tag

Currently in Shopify we can create a file.js.liquid which grants access to liquid functionality.目前在 Shopify 中,我们可以创建一个 file.js.liquid 来授予对液体功能的访问权限。

<script>
var liquidData = {
  myValue: {{ product.image | asset_url }}
}
</script>

How can I use a variable in the placeme of product.image?如何在 product.image 的 placeme 中使用变量? In example:例如:

var myVar = 'something.jpg'
    var liquidData = {
      myValue: {{ myVar | asset_url }}
    }

Currently this does not work for me the path it out puts is myVar as a string not as a variable.目前这对我不起作用,它输出的路径是 myVar 作为字符串而不是变量。 I also tried concatenation and it also reads the variable name as a string.我还尝试了连接,它还将变量名读取为字符串。 Any ideas?有任何想法吗?

You must remember that liquid is executed before the DOM.您必须记住,liquid 在 DOM 之前执行。 This applies to Javascript files as well.这也适用于 Javascript 文件。

The liquid code is executed before the JS file is processed so when you create a JS variable and try to pass it to liquid is not possible since liquid finished it's execution long before the Javascript started to execute.液体代码在处理 JS 文件之前执行,因此当您创建 JS 变量并尝试将其传递给液体时是不可能的,因为液体在 Javascript 开始执行之前很久就完成了它的执行。

So to sum it up you can't pass anything from the Javascript ot Liquid, but the other way is possible.所以总而言之,你不能从 Javascript ot Liquid 传递任何东西,但另一种方式是可能的。


So back to your question.所以回到你的问题。

It should look like so:它应该是这样的:

{% assign myVar = 'something.jpg' %}
var liquidData = {
  myValue: "{{ myVar | asset_url }}"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM