Shopify:在 Javascript 中使用来自 {% schema %} 的变量

Shopify: Using variables from {% schema %} in Javascript

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

{% 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 但没有成功。

有人可以帮助我或向我推荐有关这方面的文件吗?

非常感谢!

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

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

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

之后,您可以在 Javascript 中访问它:

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

希望对大家有所帮助