Block开发和嵌套 - shopi8 中文建站教程

Block开发和嵌套

摘要

09 Block开发和嵌套

先判断问题出现在哪里

如果 Section 是页面的积木,那么 Block 就是 Section 内部的微型积木。

Block开发和嵌套的四项 Shopify 检查清单
Block开发和嵌套的四项 Shopify 检查清单

核心逻辑是:提供颗粒度更细的排版自由。

想象一个“图文介绍” Section。如果你把标题、副标题、按钮全写死在 Section 的 Settings 里,商家就无法改变它们的上下顺序,也无法决定隐藏副标题。通过引入 Blocks,你可以让标题、文本、按钮变成一个个独立的子模块,商家可以在这个 Section 内部自由拖拽排序、增加或删除这些元素。

实战步骤

步骤 1:在 Schema 中定义 Blocks

操作路径在 section 的 {% schema %} 中添加 blocks 数组

  1. Blocks 的定义方式与 Section Settings 非常相似,但它们被包裹在 blocks 数组中。
    "blocks": [
      {
        "type": "heading",
        "name": "标题区块",
        "limit": 1, // 限制该类型 Block 最多只能添加 1 个
        "settings": [
          {
            "type": "text",
            "id": "heading_text",
            "label": "标题文字"
          }
        ]
      },
      {
        "type": "button",
        "name": "按钮区块",
        "settings": [
          {
            "type": "url",
            "id": "btn_link",
            "label": "按钮链接"
          }
        ]
      }
    ]

步骤 2:在前端循环渲染 Blocks

操作路径在 HTML 中使用 for 循环和 case 语句

  1. 商家在后台添加的 Blocks 顺序,保存在 section.blocks 数组中。
    <div class="section-wrapper">
      {% for block in section.blocks %}
        {% case block.type %}
          {% when 'heading' %}
            <h2 {{ block.shopify_attributes }}>{{ block.settings.heading_text }}</h2>
          {% when 'button' %}
            <a href="{{ block.settings.btn_link }}" {{ block.shopify_attributes }}>点击这里</a>
        {% endcase %}
      {% endfor %}
    </div>
  2. 核心魔法:{{ block.shopify_attributes }} 必须加在每个 block 的最外层 HTML 标签上。它允许 Shopify 后台编辑器识别这个 DOM 元素,实现点击左侧菜单时,右侧预览区自动滚动并高亮选中该区块。

步骤 3:配置默认的 Blocks (Presets)

操作路径在 presets 数组中预设 blocks

  1. 当商家第一次把这个 Section 拖到页面上时,你希望它默认就带有一个标题和一个按钮,而不是空空如也。
    "presets": [
      {
        "name": "图文介绍",
        "blocks": [
          { "type": "heading" },
          { "type": "button" }
        ]
      }
    ]

常见误区与处理方法

误区一:忘记添加 {{ block.shopify_attributes }} 导致编辑器体验极差

规避方法:这是新手最容易遗漏的细节。如果你在渲染 Block 时没有加上这个属性,前端页面看起来完全正常。但是,当商家在 Shopify 后台编辑器里点击左侧的“按钮区块”时,右侧的预览画面不会有任何反应(正常情况下应该自动滚动到按钮位置并出现蓝色高亮边框)。这会让商家觉得你的主题“卡住了”或存在 Bug。铁律:每一个 {% case block.type %} 下渲染的最外层 HTML 标签,必须无条件带上 {{ block.shopify_attributes }}

误区二:没有对 Block 数量进行限制,导致页面排版崩溃

规避方法:你设计了一个“三列服务优势”的 Section,使用 Blocks 来让商家添加优势说明。商家觉得很好用,一口气添加了 10 个 Block!结果前端的 Flexbox 或 Grid 布局直接被撑爆,文字挤成一团。在 Schema 中,必须对 Block 的数量进行严格的防御性限制。 可以在 Section 级别设置 "max_blocks": 3,或者在具体的 Block 类型中设置 "limit": 1(例如一个 Section 里只能有一个主标题)。不要考验商家的克制力,用代码锁死排版的边界。

误区三:在 Block 内部执行耗时的全局查询

规避方法:你在一个“推荐产品”的 Block 中,写了一段 {% for product in collections['all'].products %} 的代码去遍历全站所有产品。如果商家在页面上添加了 5 个这样的 Block,Shopify 服务器就会执行 5 次极其耗时的全站遍历,导致页面渲染超时(Liquid Timeout Error)。Block 应该只负责渲染自身携带的 settings 数据。 任何复杂的全局数据查询,应该在 Section 级别完成,然后将结果传递给 Block,或者直接让商家在 Block 设置中通过 product_picker 精准选择需要展示的商品。

Block开发和嵌套从判断到验证的三步执行路径
Block开发和嵌套从判断到验证的三步执行路径

FAQ

Block开发和嵌套应该先检查什么?

先在测试主题或测试页面中操作,并保留修改前版本和验证记录。不要同时改很多位置,先记录当前页面和数据,再处理最明确的问题。

需要马上安装新的 Shopify App 吗?

不一定。先判断主题现有功能、后台字段和少量代码能否解决。只有需要持续同步数据或复杂自动化时,再评估 App 的费用、脚本负担和卸载影响。

修改后怎么验证是否有效?

记录修改日期、页面 URL 和改动内容,再用实际页面、移动端、Google Search Console、Bing Webmaster Tools 或 GA4 检查结果。技术修改还要保留测试记录和回滚版本。

哪些情况不建议马上修改?

数据量太少、追踪没有配置、问题还没有复现,或者正在进行大型主题更新时,不建议一次性重做。先把问题拆开,确认影响范围后再改。

下一步阅读

📢 Share this article

Any other questions?

Our professional team is ready to answer your questions.

Was this article helpful to me?

This article is suitable for all merchants and developers who want to learn about Shopify. Whether you are a beginner just starting out with Shopify or an advanced user looking to improve your skills, you will gain practical knowledge and techniques from it. The methods in this article have all been tested and proven in practice and can be directly applied to your projects.

How can we apply the methods described in the article?

Each step in this article comes with detailed instructions and code examples, which you can directly copy and use. It's recommended to try it in a test environment first to confirm the results before applying it to the production site. If you encounter any problems during implementation, feel free to leave a comment or join our discussion group for help; we and our community members will be happy to assist you.

Can the code in the article be used directly?

Yes! All the code examples we provide have been tested and can be used directly in your Shopify theme. Remember to adjust the parameters and styles according to your actual needs. If you encounter any problems, feel free to leave a message for discussion.

How often will new content be updated?

We publish 2-3 high-quality Shopify tutorials and operational tips every week. Follow our WeChat official account or join our discussion group to get the latest content and exclusive resources first.

Can I get help if I encounter a problem?

Of course! You can leave a comment below the article or join our WeChat group to connect with 1000+ Shopify merchants and developers. We'll get back to you as soon as possible.

Ready to get started?

Follow us to get the latest Shopify tutorials and operational tips.

Join the community Contact Us