动态源
Dynamic sources 允许商家将 input settings 连接到来自资源的数据,例如产品、集合、博客、页面以及 metafields 和 metaobjects。
主题块可用的动态源
Theme blocks 是在主题级别定义的可重用块,可以嵌套。主题块的设置可以连接到以下来源的数据:
来源 | 描述 |
---|---|
模板 | 与当前渲染的模板或页面关联的资源。 |
区块 | resource input setting 作为 section schema 的一部分定义,例如产品设置和集合设置。 |
块 | 作为任意祖先块的一部分定义的 resource input setting,例如产品设置和集合设置。 |
Liquid | 显式通过 Liquid 的 content_for 标签传递到块的资源 drop。 |
访问最近的资源
主题块可以通过以下访问路径访问动态源:
// 模板资源
{{ product }}
{{ collection }}
// 区块设置
{{ section.settings.featured_product }}
// 块设置
{{ block.settings.collection }}
主题块是可重用的,可以在不同区块和块之间嵌套。因此,无法提前确定数据的来源。为了解决这个问题,可以使用 closest
访问路径来引用指定类型的最近资源。
closest
访问路径提供了一种方式,无论资源是来自模板、父区块还是任何祖先块,都可以访问指定类型的最近资源。
注意
closest.<type>
仅可通过主题设置访问,并可作为这些设置的配置值。
示例
在此示例中,Image banner
区块包含一个嵌套的 Product card
块,其内部有 Media
、Title
和 Price
块。它们连接到最近的产品,当前解析为 Product card
的产品设置,该设置为 Sunglasses
。
如果从 Product card
中移除 Sunglasses
产品,连接到最近产品的嵌套块仍指向 Product card
的产品设置,即使该设置为空。由于当前未设置最近的产品,嵌套块将显示占位符。
如果在 Product card
块中选择 T-shirt
产品,其嵌套块将显示新的最近产品:T-shirt
。
换句话说,连接到最近产品的设置会自动沿着其祖先链向上查找,按以下顺序获取最近产品的属性:
- 同一块内的产品设置;
- 父块的产品设置;
- 当前区块的产品设置;
- 当前模板的产品。
在主题块预设中的使用
您可以在预设中将设置连接到特定类型的最近资源,从而访问最相关的资源(如产品、集合或页面)。
您可以配置 theme block preset settings,通过以下 Liquid 语法引用兼容资源类型的最近资源:{{closest.<type>}}
。
当嵌套主题块连接到指定类型的最近资源时,最近资源的解析顺序基于以下规则:
- 指定类型的最近 Liquid 上下文设置器;
- 指定类型的最近祖先块设置;
- 指定类型的区块资源设置;
- 如果是指定类型,则为模板资源。
下表显示了每种资源类型的所有可能配置值:
资源类型 | 配置值 |
---|---|
产品 | {{ closest.product }} |
集合 | {{ closest.collection }} |
文章 | {{ closest.article }} |
博客 | {{ closest.blog }} |
页面 | {{ closest.page }} |
元对象 | {{ closest.metaobject[<definition_type>] }} |
以下示例展示了一个带有 product setting 的价格块。价格块的产品设置设置为 closest.product
。closest
关键字表示它将能够从最近的祖先中访问最近的产品资源。
{{ block.settings.product.price | default: 1999 }}
{% schema %}
{
"name": "Product (price)",
"class": "price-block",
"settings": [{
"type": "product",
"id": "product",
"label": "Product"
}],
"presets": [{
"name": "Product (price)",
"settings": {
"product": "{{ closest.product }}"
}
}]
}
{% endschema %}
您可以配置主题块预设中的设置,使其指向特定资源类型的最近祖先,而无需指定具体位置。在祖先层级连接资源后,子块可以通过其设置中的动态源访问可用资源的 属性。
在以下示例中,文本、价格和产品媒体块通过 {{ closest.product }}
访问最近的产品资源:
{% schema %}
{
"name": "Card",
"blocks": [{"type": "@theme"}],
"presets": [
{
"name": "Product Card",
"blocks": [
{
"type": "group",
"blocks": [
{
"type": "text",
"settings": {
"text": "<p>{{ closest.product.title }}</p>"
}
},
{
"type": "price",
"settings": {
"product": "{{ closest.product }}"
}
},
{
"type": "product-medias",
"settings": {
"product": "{{ closest.product }}"
}
}
]
}
]
}
]
}
{% endschema %}
预设插入到区块或块后,数据以以下 JSON 格式存储:
{
"sections": {
"custom_section_1": {
"type": "custom-section",
"blocks": {
"group_1": {
"blocks": {
"text_1": {
"type": "text",
"settings": {
"text": "<p>{{ closest.product.title }}</p>"
}
},
"price_1": {
"type": "price",
"settings": {
"product": "{{ closest.product }}"
}
},
"product_medias_1": {
"type": "product-medias",
"settings": {
"product": "{{ closest.product }}"
}
}
},
"block_order": [
"text_1",
"price_1",
"product_medias_1"
]
}
},
"block_order": ["group_1"]
}
},
"order": ["custom_section_1"]
}
在 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 }}
访问产品资源。
<ul class="product-grid">
{% for product in block.settings.collection.products %}
<li>
{% content_for "block", type: "product-card", id: "card", closest.product: product %}
</li>
{% endfor %}
</ul>
{% schema %}
{
"name": "Product grid",
"settings": [{
"type": "collection",
"id": "collection",
"label": "Collection"
}],
"presets": [{
"name": "Product grid",
"blocks": [
{
"id": "card",
"type": "product-card",
"static": true,
"settings": {
"product": "{{ closest.product }}"
},
"blocks": [
{
"type": "price",
"settings": {
"product": "{{ closest.product }}"
}
}
]
}
]
}]
}
{% endschema %}
在此示例中,我们正在渲染一个名为 product-card
的 static block,同时循环遍历 Product grid
块的集合设置中的产品。这将允许 Product card
块及其所有嵌套块通过 {{ closest.product }}
访问该卡片的当前产品。
在主题编辑器中使用动态源
商家可以使用主题编辑器中的动态源选择器,将主题块设置连接到特定类型的最近资源。商家可能可以访问多个类型的最近资源。
在此示例中,商家正在编辑产品模板上的文本块。商家正在将文本块的文本设置连接到动态源。商家可以从不同的资源上下文中选择:最近的产品、区块中设置的产品或模板中设置的产品。
选择最近的资源上下文后,商家可以选择数据字段。列出的所有数据字段都与文本设置兼容。