# Liquid 模板

> Liquid template 是 JSON 数据文件，规定 Shoplazza 主题页面渲染哪些 section 及其顺序，本页讲它的 schema 与各个属性。

Liquid template 允许您使用 [section](/docs/theme/architecture/sections/overview) 来控制在线商店各类页面的外观与样式。

Liquid template 是存储待渲染 section 列表及其相关设置的数据文件。商家可以通过主题编辑器添加、移除和重新排列这些 section。

当页面使用 Liquid template 渲染时，section 将按照 order 属性指定的顺序依次渲染，section 之间没有额外的标记。

## Schema

Liquid template 必须是有效的 JSON 文件。根对象应包含以下属性：

  
    
      
        属性
      

      
        类型
      

      
        是否必填
      

      
        描述
      
    
  

  
    
      
        `content_for_page`
      

      
        Array
      

      
        是
      

      
        section ID 的数组，按照渲染顺序排列。这些 ID 必须在 section 对象中存在，不允许重复。
      
    

    
      
        `layout`
      

      
        String
      

      
        否
      

      
        渲染 template 时使用的 [layout](../layouts/overview) 文件名。例如，指定 `"password"` 将渲染 `layout/password.liquid`。

        默认 layout 为 `theme.liquid`。  

        使用 `false` 值可在不使用 layout 的情况下渲染 template。不使用 layout 的 template 无法在主题编辑器中自定义。
      
    

    
      
        `sections`
      

      
        Object
      

      
        是
      

      
        以 section ID 为键、section 数据为值的对象。该属性至少需要包含一个 section。  

        template 内不允许有重复的 ID。  

        section 数据的格式与 [settings\_data.json](../config/settings-data-json) 中的 section 数据格式相同。更多信息请参考 [Section 数据](./liquid-templates#section-数据)。
      
    
  

### Section 数据

Liquid template 的 `sections` 属性存储了 template 需要渲染的各个 section 的数据。这些 section 可以是 [theme section](/docs/theme/architecture/sections/overview) 或 [app section](../sections/app-blocks-for-themes)。由于无法在 Liquid theme template 之间共享 section 数据，每个 section 的 ID 在该 template 内必须唯一。

您可以通过代码或主题编辑器向 template 添加 section。

下表概述了 section 数据的格式：

| 值               | 类型       | 是否必填 | 描述                                                                     |
| :--------------- | :--------- | :------- | :----------------------------------------------------------------------- |
| `<SectionID>`    | String     | -        | section 的唯一 ID，只接受字母数字字符。                                  |
| `<SectionType>`  | String     | 是       | 要渲染的 section 文件名（不含扩展名）。                                  |
| `<BlockType>`    | String     | 是       | 要渲染的 block 类型，在 section 文件的 schema 中定义。                   |
| `<SettingID>`    | String     | -        | 设置的 ID，在 section 或 block 的 schema 中定义。                        |
| `<SettingValue>` | (多种类型) | -        | 设置的有效值。                                                           |

例如：

```json title="模板"
{
  "sections": {
    "<SectionID>": {
      "type": "<SectionType>",
      "settings": {
        "<SettingID>": "<SettingValue>"
      },
      "blocks": [
        {
          "type": "<BlockType>",
          "settings": {
            "<SettingID>": "<SettingValue>"
          }
        }
      ]
    }
  }
}
```

例如，以下 template 在商品页面上渲染 `product_detail.liquid` 和 `product_description.liquid` section 文件：

```json title="templates/product.json"
{
  "content_for_page": [
    "1539149753700",
    "1634491726560"
  ],
  "sections": {
    "1539149753700": {
      "type": "product_detail",
      "settings": {
        "mobile_gallery_layout": "slide_show",
        "show_thumb_mb": true
      }
    },
    "1634491726560": {
      "type": "product_description",
      "settings": {
        "open_full_page": false,
        "spacing": 20
      }
    }
  }
}
```

:::warning
template 中包含的所有 section（非 app section）必须存在于 theme 中。否则将导致错误。
:::
