动态源

Dynamic sources 允许商家将 input settings 连接到来自资源的数据,例如产品、集合、博客、页面以及 metafieldsmetaobjects

主题块可用的动态源

Theme blocks 是在主题级别定义的可重用块,可以嵌套。主题块的设置可以连接到以下来源的数据:

来源 描述
模板 与当前渲染的模板或页面关联的资源。
区块 resource input setting 作为 section schema 的一部分定义,例如产品设置和集合设置。
作为任意祖先块的一部分定义的 resource input setting,例如产品设置和集合设置。
Liquid 显式通过 Liquid 的 content_for 标签传递到块的资源 drop。

访问最近的资源

主题块可以通过以下访问路径访问动态源:

  1. // 模板资源
  2. {{ product }}
  3. {{ collection }}
  4. // 区块设置
  5. {{ section.settings.featured_product }}
  6. // 块设置
  7. {{ block.settings.collection }}

主题块是可重用的,可以在不同区块和块之间嵌套。因此,无法提前确定数据的来源。为了解决这个问题,可以使用 closest 访问路径来引用指定类型的最近资源。

closest 访问路径提供了一种方式,无论资源是来自模板、父区块还是任何祖先块,都可以访问指定类型的最近资源。

注意

closest.<type> 仅可通过主题设置访问,并可作为这些设置的配置值。

示例

在此示例中,Image banner 区块包含一个嵌套的 Product card 块,其内部有 MediaTitlePrice 块。它们连接到最近的产品,当前解析为 Product card 的产品设置,该设置为 Sunglasses

如果从 Product card 中移除 Sunglasses 产品,连接到最近产品的嵌套块仍指向 Product card 的产品设置,即使该设置为空。由于当前未设置最近的产品,嵌套块将显示占位符。

如果在 Product card 块中选择 T-shirt 产品,其嵌套块将显示新的最近产品:T-shirt

换句话说,连接到最近产品的设置会自动沿着其祖先链向上查找,按以下顺序获取最近产品的属性:

  1. 同一块内的产品设置;
  2. 父块的产品设置;
  3. 当前区块的产品设置;
  4. 当前模板的产品。

在主题块预设中的使用

您可以在预设中将设置连接到特定类型的最近资源,从而访问最相关的资源(如产品、集合或页面)。

您可以配置 theme block preset settings,通过以下 Liquid 语法引用兼容资源类型的最近资源:{{closest.<type>}}

当嵌套主题块连接到指定类型的最近资源时,最近资源的解析顺序基于以下规则:

  1. 指定类型的最近 Liquid 上下文设置器;
  2. 指定类型的最近祖先块设置;
  3. 指定类型的区块资源设置;
  4. 如果是指定类型,则为模板资源。

下表显示了每种资源类型的所有可能配置值:

资源类型 配置值
产品 {{ closest.product }}
集合 {{ closest.collection }}
文章 {{ closest.article }}
博客 {{ closest.blog }}
页面 {{ closest.page }}
元对象 {{ closest.metaobject[<definition_type>] }}

以下示例展示了一个带有 product setting 的价格块。价格块的产品设置设置为 closest.productclosest 关键字表示它将能够从最近的祖先中访问最近的产品资源。

  1. {{ block.settings.product.price | default: 1999 }}
  2. {% schema %}
  3. {
  4. "name": "Product (price)",
  5. "class": "price-block",
  6. "settings": [{
  7. "type": "product",
  8. "id": "product",
  9. "label": "Product"
  10. }],
  11. "presets": [{
  12. "name": "Product (price)",
  13. "settings": {
  14. "product": "{{ closest.product }}"
  15. }
  16. }]
  17. }
  18. {% endschema %}

您可以配置主题块预设中的设置,使其指向特定资源类型的最近祖先,而无需指定具体位置。在祖先层级连接资源后,子块可以通过其设置中的动态源访问可用资源的 属性

在以下示例中,文本、价格和产品媒体块通过 {{ closest.product }} 访问最近的产品资源:

  1. {% schema %}
  2. {
  3. "name": "Card",
  4. "blocks": [{"type": "@theme"}],
  5. "presets": [
  6. {
  7. "name": "Product Card",
  8. "blocks": [
  9. {
  10. "type": "group",
  11. "blocks": [
  12. {
  13. "type": "text",
  14. "settings": {
  15. "text": "<p>{{ closest.product.title }}</p>"
  16. }
  17. },
  18. {
  19. "type": "price",
  20. "settings": {
  21. "product": "{{ closest.product }}"
  22. }
  23. },
  24. {
  25. "type": "product-medias",
  26. "settings": {
  27. "product": "{{ closest.product }}"
  28. }
  29. }
  30. ]
  31. }
  32. ]
  33. }
  34. ]
  35. }
  36. {% endschema %}

预设插入到区块或块后,数据以以下 JSON 格式存储:

  1. {
  2. "sections": {
  3. "custom_section_1": {
  4. "type": "custom-section",
  5. "blocks": {
  6. "group_1": {
  7. "blocks": {
  8. "text_1": {
  9. "type": "text",
  10. "settings": {
  11. "text": "<p>{{ closest.product.title }}</p>"
  12. }
  13. },
  14. "price_1": {
  15. "type": "price",
  16. "settings": {
  17. "product": "{{ closest.product }}"
  18. }
  19. },
  20. "product_medias_1": {
  21. "type": "product-medias",
  22. "settings": {
  23. "product": "{{ closest.product }}"
  24. }
  25. }
  26. },
  27. "block_order": [
  28. "text_1",
  29. "price_1",
  30. "product_medias_1"
  31. ]
  32. }
  33. },
  34. "block_order": ["group_1"]
  35. }
  36. },
  37. "order": ["custom_section_1"]
  38. }

在 Liquid 中通过 closest 向下传递资源

主题块设置可以直接访问 Liquid 中通过 content_for Liquid 标签 设置的最近资源。资源可以通过指定资源类型的参数直接传递到 content_for 标签。

下表显示了哪些资源类型可以通过 Liquid 中的 content_for 标签传递:

资源类型 语法
产品 {% content_for "blocks", closest.product: <Product Drop> %}
集合 {% content_for "blocks", closest.collection: <Collection Drop> %}
文章 {% content_for "blocks", closest.article: <ArticleDrop> %}
博客 {% content_for "blocks", closest.blog: <BlogDrop> %}
页面 {% content_for "blocks", closest.page: <PageDrop> %}
元对象 {% content_for "blocks", closest.metaobject.<definition_type>: <MetaobjectDrop> %}

用于渲染 static blocks{% content_for "block" %} 标签也支持使用相同的语法传递上下文。

注意

如果资源类型不匹配这些参数中的任何一个,则会返回错误。

示例

在 Liquid 中向嵌套块传递资源的常见用例是在集合中循环遍历产品时。我们需要将当前正在迭代的产品传递到 content_for 标签中,以便嵌套块可以使用 {{ closest.product }} 访问产品资源。

  1. <ul class="product-grid">
  2. {% for product in block.settings.collection.products %}
  3. <li>
  4. {% content_for "block", type: "product-card", id: "card", closest.product: product %}
  5. </li>
  6. {% endfor %}
  7. </ul>
  8. {% schema %}
  9. {
  10. "name": "Product grid",
  11. "settings": [{
  12. "type": "collection",
  13. "id": "collection",
  14. "label": "Collection"
  15. }],
  16. "presets": [{
  17. "name": "Product grid",
  18. "blocks": [
  19. {
  20. "id": "card",
  21. "type": "product-card",
  22. "static": true,
  23. "settings": {
  24. "product": "{{ closest.product }}"
  25. },
  26. "blocks": [
  27. {
  28. "type": "price",
  29. "settings": {
  30. "product": "{{ closest.product }}"
  31. }
  32. }
  33. ]
  34. }
  35. ]
  36. }]
  37. }
  38. {% endschema %}

在此示例中,我们正在渲染一个名为 product-cardstatic block,同时循环遍历 Product grid 块的集合设置中的产品。这将允许 Product card 块及其所有嵌套块通过 {{ closest.product }} 访问该卡片的当前产品。

在主题编辑器中使用动态源

商家可以使用主题编辑器中的动态源选择器,将主题块设置连接到特定类型的最近资源。商家可能可以访问多个类型的最近资源。

在此示例中,商家正在编辑产品模板上的文本块。商家正在将文本块的文本设置连接到动态源。商家可以从不同的资源上下文中选择:最近的产品、区块中设置的产品或模板中设置的产品。

动态 Sources - 图1

选择最近的资源上下文后,商家可以选择数据字段。列出的所有数据字段都与文本设置兼容。

动态 Sources - 图2