简体   繁体   English

我需要一种变通方法来将带有破折号的Liquid变量分配给javascript变量名称

[英]I need a workaround for assigning Liquid variables with dashes to javascript variable names

I have a short form on my product pages for customers to submit their email address. 我的产品页面上有一个简短的表格,供客户提交其电子邮件地址。 Javascript is used to assign custom properties alongside the email address in the Klaviyo email system I use to manage email subscriptions. Javascript用于在我用来管理电子邮件订阅的Klaviyo电子邮件系统中,在电子邮件地址旁边分配自定义属性。

I have been able to use Liquid output to send some Shopify product information - such as the product ID - as one of these custom properties. 我已经能够使用Liquid输出将一些Shopify产品信息(例如产品ID)作为这些自定义属性之一发送。 However, I would like to send a SKU, handle or product name, which does not work because this data is stored using a dash as a delimiter. 但是,我想发送一个SKU,句柄或产品名称,该名称不起作用,因为此数据是使用破折号作为分隔符来存储的。 This causes an issue with the Javascript parser 这会导致Javascript解析器出现问题

<script type="text/javascript">
    KlaviyoSubscribe.attachToForms('#email_signup', {
        hide_form_on_success: true,
        custom_success_message: true,
        custom_error_message: true,
        extra_properties:{ //Fully customisable - call them what you want
            PROPERTY_{{ Shopify.Liquid.Variable }}:true
        }
    });
</script> 

In the example above PROPERTY_{{ product.id }}:true would be fine. 在上面的示例中, PROPERTY_{{ product.id }}:true是可以的。 This would resolve to something like PROPERTY_123456789:true which is parsed properly and transmitted. 这将解析为类似于PROPERTY_123456789:true ,该内容将被正确解析并传输。

However PROPERTY_{{ product.handle }}:true would become something like PROPERTY_product-handle-example:true 但是, PROPERTY_{{ product.handle }}:true会变成PROPERTY_product-handle-example:true

The dashes here don't parse in javascript. 此处的破折号不在javascript中解析。 I've been playing around trying to put the liquid variable into a JS string or something but not getting it. 我一直在尝试将Liquid变量放入JS字符串之类的东西,但没有得到它。 I'm sure it's simple but I can't see the wood for the trees on this one. 我敢肯定这很简单,但是我看不到这棵树上的木头。

You may use the JSON filter: 您可以使用JSON过滤器:

{{ Shopify.Liquid.Variable | json }}

As explained here: https://help.shopify.com/en/themes/liquid/filters/additional-filters#json 如此处所述: https : //help.shopify.com/en/themes/liquid/filters/additional-filters#json

HTH HTH

I am going to guess your parser really handles the page before the javascript gets send to the client, but I guess to have it correct from your point of view, you could just use ['property'] . 我想您的解析器在将javascript发送到客户端之前确实可以处理该页面,但是我想从您的角度来看它是正确的,您可以使用['property']

It should be fine from a preparser point of view 从准备者的角度来看应该没问题

KlaviyoSubscribe.attachToForms('#email_signup', {
    hide_form_on_success: true,
    custom_success_message: true,
    custom_error_message: true,
    extra_properties:{ //Fully customisable - call them what you want
        ['PROPERTY_{{ Shopify.Liquid.Variable }}']:true
    }
});

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

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