Templates allow editors to centrally control content and reuse content. They allow a developer to not have to hard code layout decisions and instead use rules to apply user layouts in template content stored separately from the page, or give the user a choice on which layout they want.
Templates:
Templates are analogous to blocks themselves but are made up of blocks with special properties:
Configure templates in page.schema.properties on the blocks field:
Use expandTemplates (async) or expandTemplatesSync (sync with pre-fetched templates) to merge template content during rendering.
Sync vs Async:
loadTemplates(data, loadTemplate) scans page data for templateId references and loads them all in parallel. It follows nested references (templates referencing other templates) and has a 5s per-template timeout. It only loads templates actually in the page data — allowedLayouts options are loaded on demand when a forced layout is applied.
Options:
The merge algorithm follows these rules:
When a layout is applied, the rules are the same but applied across a whole blocks field. Content without a placeholder name ends up:
Your frontend might want to force a layout to apply regardless of whether one is saved, for example to ensure a footer layout. Pass allowedLayouts:
Note: during editing the admin side will load the templates so in order to apply the same rules of forcing a layout you will need to set allowedLayouts in page.schema.properties to ensure the page loads with the right template.