Section schema(区块模式)

Section 支持 {% schema %} Liquid 标签。这个标签允许你定义一个区块的各种属性,比如区块名称、区块内的子模块(block)、以及供主题编辑器自定义使用的设置项。

Schema(结构)

每个 Section 只能包含一个 {% schema %} 标签,并且里面必须是有效的 JSON,只允许使用 Content(内容) 部分列出的属性。
{% schema %} 标签可以放在 Section 文件中的任何地方,但不能嵌套在其他 Liquid 标签内部

注意事项

  • 如果有多个 {% schema %} 标签,或者把 {% schema %} 放在其他 Liquid 标签里面,会导致报错。

以下是一个合法的 Section schema 示例。每个属性的详细说明请参考 Content(内容)

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "tag": "section",
  5. "class": "slideshow",
  6. "limit": 1,
  7. "settings": [
  8. {
  9. "type": "text",
  10. "id": "title",
  11. "label": "Slideshow"
  12. }
  13. ],
  14. "max_blocks": 5,
  15. "blocks": [
  16. {
  17. "name": "Slide",
  18. "type": "slide",
  19. "settings": [
  20. {
  21. "type": "image_picker",
  22. "id": "image",
  23. "label": "Image"
  24. }
  25. ]
  26. }
  27. ],
  28. "presets": [
  29. {
  30. "name": "Slideshow",
  31. "settings": {
  32. "title": "Slideshow"
  33. },
  34. "blocks": [
  35. {
  36. "type": "slide"
  37. },
  38. {
  39. "type": "slide"
  40. }
  41. ]
  42. }
  43. ],
  44. "locales": {
  45. "en": {
  46. "title": "Slideshow"
  47. },
  48. "fr": {
  49. "title": "Diaporama"
  50. }
  51. },
  52. "enabled_on": {
  53. "templates": ["*"],
  54. "groups": ["footer"]
  55. }
  56. }
  57. {% endschema %}

内容(Content)

{% schema %} 里面可以包含以下属性:

此外,还可以使用以下属性来控制 Section 的适用范围:

  • enabled_on:限定 Section 只能用于指定模板或分组
  • disabled_on:禁止 Section 用于指定模板或分组

注意

  • {% schema %} 是 Liquid 标签,但它不会输出内容,也不会解析内部的 Liquid 语法。
  • 你还应该考虑让你的 Section 兼容 app blocks(应用区块),这样开发者可以让商家在不直接修改代码的情况下将应用内容添加到主题中。

各属性详细说明

name

name 属性定义了区块在主题编辑器中的标题。例如:

  1. {% schema %}
  2. {
  3. "name": "Slideshow"
  4. }
  5. {% endschema %}

效果:

示例


tag

默认情况下,Shopify 渲染 Section 时会用 <div> 标签包裹,并加上唯一的 id

  1. <div id="shopify-section-[id]" class="shopify-section">
  2. // 区块内容
  3. </div>

如果你不想使用 <div>,可以通过 tag 属性指定 HTML 标签。支持的值有:

  • article
  • aside
  • div
  • footer
  • header
  • section

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "tag": "section"
  5. }
  6. {% endschema %}

生成:

  1. <section id="shopify-section-[id]" class="shopify-section">
  2. // 区块内容
  3. </section>

class

默认 Section 渲染时有 shopify-section 类名。你可以通过 class 属性额外添加类名:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "tag": "section",
  5. "class": "slideshow"
  6. }
  7. {% endschema %}

生成:

  1. <section id="shopify-section-[id]" class="shopify-section slideshow">
  2. // 区块内容
  3. </section>

limit

默认情况下,同一个 Section 可以在模板或分组中添加多次。
你可以通过 limit 限制只能添加 1 次或 2 次:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "tag": "section",
  5. "class": "slideshow",
  6. "limit": 1
  7. }
  8. {% endschema %}

settings

通过 settings 数组,你可以定义该 Section 支持哪些自定义设置。

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "tag": "section",
  5. "class": "slideshow",
  6. "settings": [
  7. {
  8. "type": "text",
  9. "id": "header",
  10. "label": "Header"
  11. }
  12. ]
  13. }
  14. {% endschema %}

注意事项

  • 每个设置的 id 必须是唯一的。
  • 否则会导致错误。

如何访问 settings

可以通过 section 对象 获取设置的值。


blocks

区块可以包含子模块(blocks),比如轮播图的每一张幻灯片。

每个 block 定义如下属性:

属性 描述 是否必填
type Block 类型,自定义的标识符
name Block 名称,出现在主题编辑器里
limit 限制这种 block 类型的数量
settings 这个 block 的设置项

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "blocks": [
  5. {
  6. "name": "Slide",
  7. "type": "slide",
  8. "settings": [
  9. {
  10. "type": "image_picker",
  11. "id": "image",
  12. "label": "Image"
  13. }
  14. ]
  15. }
  16. ]
  17. }
  18. {% endschema %}

注意事项

  • 每个 block 的 nametype 都要在同一个 section 内部唯一。
  • 每个 setting 的 id 在 block 内部也必须唯一。

如何渲染 blocks

通过循环遍历 section.blocks 来渲染所有 block:

  1. {% for block in section.blocks %}
  2. {% case block.type %}
  3. {% when 'slide' %}
  4. <div class="slide" {{ block.shopify_attributes }}>
  5. {{ block.settings.image | image_url: width: 2048 | image_tag }}
  6. </div>
  7. ...
  8. {% endcase %}
  9. {% endfor %}

注意事项

  • 不要依赖 block 的 id 固定值去写判断,因为 id 是动态生成的,随时可能变化。

错误示例:

  1. {% for block in section.blocks %}
  2. {% if block.id == 'J6d9jV' %}
  3. <h1>{{ block.settings.heading }}</h1>
  4. {% endif %}
  5. {% endfor %}

动态显示 block 标题

如果一个 block 的设置项有以下 id 名称之一,则在主题编辑器中,这个设置项的值会作为 block 的标题展示(按优先顺序):

  1. heading
  2. title
  3. text

如果没有,才使用 block 的 name

示例:

  1. "blocks": [
  2. {
  3. "name": "Announcement",
  4. "type": "announcement",
  5. "settings": [
  6. {
  7. "type": "text",
  8. "id": "heading",
  9. "default": "Welcome to our store",
  10. "label": "Heading"
  11. }
  12. ]
  13. }
  14. ]

效果是 sidebar 中标题显示为 Welcome to our store


max_blocks

每个 Section 最多可以添加 50 个 block。
你可以通过 max_blocks 手动设置更低的上限。

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "max_blocks": 5
  5. }
  6. {% endschema %}

静态区块(static blocks)不会占用这个数量限制。


presets

Presets 是 Section 的默认配置,用于让用户在编辑器里快速添加一个预设好的区块。

Presets 属性:

属性 描述 是否必填
name 预设的名称
settings 默认设置的值列表
blocks 默认包含哪些 blocks

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "presets": [
  5. {
  6. "name": "Slideshow",
  7. "settings": {
  8. "title": "Slideshow"
  9. },
  10. "blocks": [
  11. {
  12. "type": "slide"
  13. },
  14. {
  15. "type": "slide"
  16. }
  17. ]
  18. }
  19. ]
  20. }
  21. {% endschema %}

default

default 属性用于为静态区块(static blocks)设置默认内容。

静态区块是指那些在代码中固定写死,而不是通过主题编辑器自由添加的 block。
default 会自动应用到模板中静态 block 上,以提供默认数据。

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "blocks": [
  5. {
  6. "type": "slide",
  7. "name": "Slide",
  8. "settings": [
  9. {
  10. "type": "text",
  11. "id": "heading",
  12. "label": "Heading",
  13. "default": "Default heading"
  14. }
  15. ]
  16. }
  17. ],
  18. "default": {
  19. "blocks": {
  20. "static-slide": {
  21. "type": "slide",
  22. "settings": {
  23. "heading": "A default heading"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. {% endschema %}

对应的静态 block 应该在 Section 的 Liquid 模板中这样写:

  1. {% block 'slide' id: 'static-slide' %}
  2. <!-- 静态幻灯片内容 -->
  3. {% endblock %}

注意事项

  • default 只会应用在静态 block 上,不影响动态 block。
  • default 的 key(例子里是 static-slide)必须和 id 保持一致。

locales

locales 属性用于给不同语言环境设置文本翻译。

它是一个对象,每个语言代码(比如 enfr)下定义一组键值对。

示例:

  1. {% schema %}
  2. {
  3. "name": "Slideshow",
  4. "locales": {
  5. "en": {
  6. "title": "Slideshow"
  7. },
  8. "fr": {
  9. "title": "Diaporama"
  10. }
  11. }
  12. }
  13. {% endschema %}

这样在不同的语言环境下,区块的标题(title)就能显示对应翻译。


控制 Section 的可用范围

有时你希望某些 Section 只在特定页面、特定区域可见。
可以使用 enabled_ondisabled_on 来限制 Section 的适用范围。


enabled_on

enabled_on 限定只能在指定模板或分组里添加这个 Section。
格式:

  1. "enabled_on": {
  2. "templates": ["template1", "template2"],
  3. "groups": ["header", "footer"]
  4. }

支持的 templates

  • 使用模板文件名,比如 "product", "collection", "page.about"
  • 可以使用 "*" 通配符表示所有模板。

支持的 groups

  • header
  • footer

示例:

  1. {% schema %}
  2. {
  3. "name": "Footer Banner",
  4. "enabled_on": {
  5. "templates": ["*"],
  6. "groups": ["footer"]
  7. }
  8. }
  9. {% endschema %}

表示这个 Section 只能添加到「Footer」区域。


disabled_on

disabled_onenabled_on 相反,表示禁止在指定模板或分组里添加这个 Section。

示例:

  1. {% schema %}
  2. {
  3. "name": "Homepage Slideshow",
  4. "disabled_on": {
  5. "templates": ["product", "collection"]
  6. }
  7. }
  8. {% endschema %}

表示这个 Section 无法在商品页(product)和集合页(collection)中使用。

App blocks

如果你的 section 是 JSON templatesection group 的一部分,那么你应该支持类型为 @app 的 block。 App blocks 允许应用开发者创建 block,让商家可以把应用内容添加到他们的主题里,而不需要直接编辑主题代码。

小结

  • Section 只能有一个 {% schema %} 标签,且必须是有效的 JSON
  • settings 定义了可以自定义的选项,blocks 定义了子模块(比如幻灯片)。
  • presets 提供默认的区块布局,方便用户快速添加。
  • enabled_ondisabled_on 控制这个 Section 在主题编辑器中出现的位置。
  • 记得用 locales 来做好国际化!