【问题标题】:Shopify: Using variables from {% schema %} in JavascriptShopify:在 Javascript 中使用来自 {% schema %} 的变量
【发布时间】:2018-05-05 20:15:38
【问题描述】:

我有一个使用以下架构的自定义部分:

{% schema %}
  {
    "name": "Custom",
    "settings": [
    {
        "type": "textarea",
        "id": "custom_text_product",
        "label": "Insert name of the product here",
        "default": "Product"
      },
    {
        "type": "textarea",
        "id": "custom_text_msg",
        "label": "Custom text",
        "default": "Insert text here"
      }
    ]
  }
{% endschema %}

基本上我想要的是从每个文本区域获取文本,通过 Javascript 进行操作,然后将其添加到 DOM。

通过.liquid 我只会做{{ section.settings.id }},但我不知道如何在Javascript 中访问它们。因为它是一个大文本,我也不能将它作为数据属性添加到 DOM。

我尝试关注this,但没有成功。

有人可以帮助我或让我参考有关此的文件吗?

非常感谢!

【问题讨论】:

    标签: javascript shopify liquid


    【解决方案1】:

    好的,经过大量研究和 cmets 说这是不可能的,我找到了一种方法。

    如果您使用的是 .liquid 文件,您希望将 {% schema %} 变量分配给本地 .liquid 变量,如下所示:

    {%- assign product_text = section.settings.custom_text_product -%}
    

    之后,您可以通过以下方式在 Javascript 中访问它:

    <script>
      var productText = `{{ product_text }}`;
    </script>
    

    希望对大家有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2021-11-20
      • 2012-11-03
      • 1970-01-01
      相关资源
      最近更新 更多