Shopify Liquid 模板语言完全指南:从语法基础到高级进阶 - shopi8 中文建站教程

Shopify Liquid 模板语言完全指南:从语法基础到高级进阶

摘要

不懂 Liquid 就做不了 Shopify!深度解析 Shopify 专属模板语言 Liquid。从 Objects、Tags 到 Filters,掌握数据渲染、逻辑控制与数组操作的核心语法。

1. 核心底层逻辑

Liquid 不是一门真正的编程语言(如 JavaScript 或 Python),它是一种模板语言(Template Language)

Shopify Liquid 模板语言完全指南:从语法基础到高级进阶 总览图
先看这张总览图,再对照正文里的步骤、字段和检查项操作。
Shopify Liquid 模板语言完全指南:从语法基础到高级进阶 操作示意图
真实页面参考:Shopify Liquid 官方文档。对照本步骤确认当前官方路径和关键概念,实际操作以你的店铺后台、本地终端或代码仓库为准。

核心逻辑是:占位符替换与安全的数据渲染。

Shopify 的服务器在将 HTML 发送给用户浏览器之前,会先解析 Liquid 代码。它将数据库中的动态数据(如产品标题、价格)填入占位符中,并执行简单的逻辑判断(如“如果缺货则显示售罄”)。Liquid 运行在服务器端,因此它很安全,无法直接操作 DOM,也无法发起网络请求。

2. Step-by-Step 操作指南

核心语法 1:Objects (对象) 与输出

操作路径使用双大括号 {{ }} 输出数据

  1. Objects 包含了 Shopify 数据库中的所有信息。
  2. 输出产品标题:<h1>{{ product.title }}</h1>
  3. 输出产品价格:<span>{{ product.price | money }}</span>
  4. 注意:对象是有作用域(Scope)的。product 对象在产品页(product.liquid)中默认可用,但在首页(index.liquid)中直接调用会返回空值,除非你在循环中遍历了产品列表。

核心语法 2:Tags (标签) 与逻辑控制

操作路径使用大括号加百分号 {% %} 执行逻辑

  1. Tags 不会输出任何可见内容,它们用于控制页面的渲染逻辑。
  2. 条件判断 (If/Else)
    {% if product.available %}
      <button>加入购物车</button>
    {% else %}
      <button disabled>已售罄</button>
    {% endif %}
  3. 循环遍历 (For Loop)
    {% for image in product.images %}
      <img src="{{ image | image_url: width: 500 }}" alt="{{ image.alt }}">
    {% endfor %}

核心语法 3:Filters (过滤器) 与数据处理

过滤器类型 Liquid 代码示例 渲染结果说明
字符串处理 {{ 'hello world' | upcase | replace: 'WORLD', 'SHOPIFY' }} 输出:HELLO SHOPIFY (支持管道式链式调用)
数学运算 {{ product.price | times: 1.2 | divided_by: 100 }} 将价格乘以 1.2 后除以 100。
数组操作 {% assign sorted_products = collection.products | sort: 'price' %} 将集合中的产品按价格从低到高排序。

核心语法 4:变量赋值 (Assign 与 Capture)

操作路径在 Liquid 中创建自定义变量

  1. Assign:用于创建简单的字符串、数字或布尔值变量。
    {% assign discount_rate = 0.8 %}
  2. Capture:用于捕获复杂的 HTML 结构或多行字符串,将其存入一个变量中。
    {% capture button_html %}
      <div class="btn-wrapper">
        <button>{{ product.title }}</button>
      </div>
    {% endcapture %}
    {{ button_html }}

3. 2026年最新大坑与规避方法

大坑一:在 Liquid 循环中执行复杂的数学运算导致渲染超时

规避方法:Liquid 运行在 Shopify 的服务器上,为了保证所有店铺的稳定性,Shopify 对 Liquid 的渲染时间有严格的限制。如果你在一个包含 1000 个产品的集合页中,使用 {% for %} 循环遍历每个产品,并在循环内部使用 timesdivided_by 去计算复杂的阶梯折扣,极易导致页面渲染超时并报错。复杂的数学运算应该在后台通过脚本计算好并存入 Metafields,前端 Liquid 只负责直接读取和展示。

大坑二:混淆了空白符控制 (Whitespace Control)

规避方法:当你在 HTML 中写了大量的 {% if %}{% for %} 标签时,即使这些标签不输出内容,它们也会在最终的 HTML 源码中留下大量的空行和换行符,导致 DOM 结构臃肿。必须学会使用连字符 - 来剥离空白符。 例如,写成 {%- if product.available -%} 而不是 {% if product.available %}。连字符会吃掉标签左侧或右侧的所有空白字符,让输出的 HTML 源码很干净紧凑。

大坑三:试图用 Liquid 过滤敏感数据 (如客户密码)

规避方法:新手经常问:“我怎么用 Liquid 获取当前登录客户的密码或完整信用卡号?”答案是:绝对不可能。 Liquid 的设计初衷就是为了前端展示,Shopify 在底层严格隔离了所有敏感数据。你只能通过 customer 对象获取客户的名字、邮箱、订单历史和默认地址。任何涉及支付、密码修改的逻辑,都必须跳转到 Shopify 托管的封闭页面处理。

常见问题

学习「Shopify Liquid 模板语言完全指南:从语法基础到高级进阶」前需要什么基础?

建议先熟悉 HTML、CSS、基础 JavaScript 和 Shopify 后台结构。涉及 Liquid、Section、Schema 或主题工作流的内容,可以边读边在测试主题里练习,不要直接改线上主题。

可以直接在正在使用的线上主题里操作吗?

不建议。主题开发和结构调整应先在复制主题、开发主题或本地环境中完成,确认移动端、产品页、购物车和关键模板正常后,再发布到线上主题。

修改主题前最应该备份什么?

至少保留当前主题副本,并用 Git 记录代码变化。如果文章涉及主题编辑器配置,还要注意模板 JSON 和 settings_data.json 这类配置文件是否需要同步。

遇到教程和后台界面不一致怎么办?

优先以当前 Shopify 后台、主题代码和官方文档为准。Shopify 后台和 CLI 会持续更新,旧截图可用于理解路径,但不能替代当前界面提示。

这类主题开发内容适合什么时候上线到正式店铺?

当改动已经在测试主题中完成移动端、桌面端、产品页、集合页、购物车和速度检查后,再安排上线。影响结账、价格、库存或应用兼容的改动要单独回归。

📢 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