# 从模板文件创建可复用图案

与页面相同的HTML，可直接插入区块插入器。

## 模板文件，而非注册代码

创建一个包含 `pattern.json` 和模板的文件夹。Blockstudio会注册它，无需 `register_block_pattern()` 样板代码。

样式会插入真正可编辑的区块，为用户提供可自定义的起始布局，同时源文件仍由版本控制。

[样式语法](https://blockstudio.dev/zh/docs/pages-and-patterns/patterns/)

index.php

```
<div>
  <h2>Pricing</h2>
  <p>Simple, transparent pricing.</p>
  <block name="core/columns">
    <block name="core/column"><h3>Starter</h3></block>
    <block name="core/column"><h3>Pro</h3></block>
  </block>
</div>
```

## 与页面语法相同

标准 HTML 会映射到核心区块；其他内容由区块标签处理。

## 自动注册

放入模板和pattern.json，它便会自动显示。

## 完全可编辑

用户可以分别自定义每个插入的样式。

## 分类且可搜索

分类和关键词让样式更容易被找到。

## 任意区块，任意嵌套层级

可在任意深度使用核心区块、自定义区块或Blockstudio区块。

## 文章类型限制

将样式限制为适用的文章类型。

## 区块关联

将样式与区块类型关联，使其作为建议显示。

## 纳入版本控制

在Git中跟踪样式文件，并将它们部署到不同环境。

## 使用pattern.json配置

在 `pattern.json` 中设置标题、描述、分类、关键词、视口宽度、文章类型和区块类型。还可在同一文件中控制其在插入器中的显示状态。

分类用于整理样式，关键词让样式可供搜索，视口宽度则控制预览效果。

[样式配置](https://blockstudio.dev/zh/docs/pages-and-patterns/patterns/)

pattern.json

```
{
  "name": "pricing-table",
  "title": "Pricing Table",
  "description": "Two-column pricing layout",
  "categories": ["featured", "commerce"],
  "keywords": ["pricing", "plans", "table"],
  "viewportWidth": 1200,
  "blockTypes": ["core/group"],
  "postTypes": ["page", "landing"]
}
```

## Blade 和 Twig 源

将区块、页面或站点编辑器模板编写为 `index.blade.php` 或 `index.twig`，而不是 `index.php`。Blade 通过 `Blockstudio\Blade_Renderer` 渲染，使用 `illuminate/view` 8 到 12，而 Twig 需要 Timber。

源代码会在没有请求上下文的情况下编译，因此不含动态变量。可将其用于引擎内置的函数和过滤器。

[模板引擎](https://blockstudio.dev/zh/docs/pages-and-patterns/#template-engines)

patterns/pricing/index.blade.php

```
<div>
  <h2>{{ strtoupper('Pricing') }}</h2>
  <p>Simple, transparent pricing.</p>
</div>
```

## 供编辑者选择的自定义模板

如果 `template.json` 的 slug 不在模板层级结构中，它就会成为自定义模板。在 `postTypes` 中列出文章类型，编辑者即可在模板选择器中选择该模板。

`Blockstudio\Site_Templates` 会读取并编译 PHP 中的源文件，因此测试和部署检查可以查看编译后的标记，并针对所需项目调用 `selection_errors()`。

[站点编辑器模板指南](https://blockstudio.dev/zh/guides/site-editor-templates/)

templates/page-landing/template.json

```
{
  "slug": "page-landing",
  "title": "Landing Page",
  "description": "Full-width page without a header",
  "postTypes": ["page"]
}
```

## 自定义发现路径

筛选器可为页面、版式和站点编辑器模板添加根目录，例如来自某个插件的根目录。

## 逻辑发现源

预览、叠加层和生成的主题可以向 Blockstudio 提供组合后的文件清单，而不是单个主题文件夹。

## 生命周期钩子

版式注册后，以及页面文章创建、更新、同步或协调后，都会触发操作。

## 版式 API

Blockstudio\Patterns::patterns() 和 get_pattern() 会读取已从 PHP 注册的版式。

Plus

## 官方扩展套件

高级站点模板、AI 系统指令和私人 Discord 社区。一次购买，终身更新。

- 500+ 个网站模板
- AI 系统指令
- 终身更新
- 私有 Discord 社区

[获取 Plus](https://blockstudio.dev/zh/plus/)
