块编辑器开发文档

站点编辑模板

💡 云策文档标注

概述

本文档解释了模板和模板部件在前端渲染和后端编辑的内部机制,重点介绍了存储、同步和主题切换等核心流程。

关键要点

  • 模板存储:块模板最初作为文件存储在主题文件夹中,用户可通过站点编辑器UI编辑,编辑后的版本保存到wp_template或wp_template_part自定义文章类型中。
  • 模板同步:通过“模板同步”操作,将主题模板复制到自定义文章类型中,状态为auto-draft,编辑后更新为publish,简化渲染和编辑算法。
  • 主题切换:当前阶段禁止混合不同主题的模板和模板部件,但通过保存theme post meta记录来源主题,为未来可能允许混合使用奠定基础。

注意事项

  • 同步确保渲染时仅需考虑自定义文章类型模板,无需直接获取主题文件。
  • 编辑模板时,站点编辑器前端通过REST API获取模板,同步对GET API请求至关重要。
  • 导出块主题时,同步简化操作,仅导出CPT模板。

📄 原文内容

Template and template part flows

This document will explain the internals of how templates and templates parts are rendered in the frontend and edited in the backend.

Storage

Just like the regular templates, the block templates live initially as files in the theme folder but the main difference is that the user can edit these templates in the UI in the Site Editor.

When a user edits a template (or template-part), the initial theme template file is kept as is but a forked version of the template is saved to the wp_template custom post type (or wp_template_part for template parts).

These capabilities mean that at any point in time, a mix of template files (from the theme) and CPT templates (the edited templates) are used to render the frontend of the site.

Synchronization

In order to simplify the algorithm used to edit and render the templates from two different places, we performed an operation called “template synchronization”.

The synchronization consists of duplicating the theme templates in the wp_template (and wp_template_part) custom templates with an auto-draft status. When a user edits these templates, the status is updated to publish.

This means:

  • The rendering/fetching of templates only need to consider the custom post type templates. It is not necessary to fetch the template files from the theme folder directly. The synchronization will ensure these are duplicated in the CPT.
  • Untouched theme templates have the auto-draft status.
  • Edited theme templates have the publish status.

The synchronization is important for two different flows:

  • When editing the template and template parts, the site editor frontend fetches the edited and available templates through the REST API. This means that for all GET API requests performed to the wp-templates and wp-template-parts endpoint synchronization is required.
  • When rendering a template (sometimes referred to as “resolving a template”): this is the algorithm that WordPress follows to traverse the template hierarchy and find the right template to render for the current page being loaded.
  • When exporting a block theme, we need to export all its templates back as files. The synchronization is required to simplify the operation and only export the CPT templates.

Switching themes

Since block themes make use of templates that can refer to each other and that can be saved to a custom post type, it becomes possible to mix templates and template parts from different themes. For example:

  • A user might like the “header” template part of theme A and would like to use it in theme B.
  • A user might like the “contact” template from theme A and would like to use it in theme B.

Enabling these flows will require well thought UIs and experience. For the current phase of Full-site editing, we’re starting by forbidding these possibilities and making template and template-parts theme specific.

That said, it is still important to keep track of where the template and template part come from initially. From which theme, it’s based. We do so by saving a theme post meta containing the theme identifier for each template and template part CPT entry.

In the future, we might consider allowing the user to mix template and template parts with different theme post meta values.