Page Templates
Page templates give a page a fixed layout. Editors fill in the fields you define, but can’t rearrange them. Use a template when free-form blocks give too much freedom, such as on an about or contact page.
Pages work like WordPress: every page has its own editable slug and picks a template from the Template dropdown in the sidebar. The boilerplate ships with one template, default, which is a free-form list of blocks.
How it works
Section titled “How it works”Each template is split across two files:
| File | Role |
|---|---|
src/templates/[name]/config.ts |
Defines the fields Payload shows in the admin panel |
src/templates/[name]/[name].tsx |
The React component that owns the layout |
Two files connect every template to the rest of the app:
| File | Role |
|---|---|
src/templates/config.ts |
Lists all template configs, used by the pages collection |
src/templates/render-template.tsx |
Maps each template to its component, used by the routes |
Creating a New Template
Section titled “Creating a New Template”-
Create the config file
src/templates/about/config.ts import { pageTemplate } from '../page-template';export const about = pageTemplate({slug: 'about',label: 'About',fields: [{ name: 'intro', type: 'textarea', required: true },{ name: 'body', type: 'richText' },],});pageTemplate()wraps the fields in a group named after the slug, and only shows that group when the template is selected. The slug becomes a field name, so write it in camelCase:caseStudy, notcase-study. -
Create the component
src/templates/about/about.tsx import RichText from '@/components/ui/rich-text/rich-text';import type { Page } from '@/payload-types';interface Props {page: Page;}export default function About({ page }: Props) {const { intro, body } = page.about ?? {};return (<main className="template-about"><div className="container"><h1 className="text-heading-lg">{page.title}</h1><p>{intro}</p>{body && <RichText data={body} />}</div></main>);}The JSX is the layout, so editors can’t change the order of these sections.
-
Register the config
src/templates/config.ts import { about } from './about/config';export const pageTemplates: PageTemplate[] = [defaultTemplate, about]; -
Register the component
src/templates/render-template.tsx import About from './about/about';const templates = {default: Default,about: About,} satisfies Record<PageType, ComponentType<{ page: Page }>>;Once types are regenerated, a template missing from this map fails to compile.
-
Generate types
Terminal window ddev pnpm generate:types -
Create a migration
Terminal window ddev pnpm payload migrate:create add_about_templateSee Database Migrations for details.
-
Select the template
Open a page in the admin panel, and choose About from the Template dropdown.
Allowing Some Blocks
Section titled “Allowing Some Blocks”To give editors limited freedom, add a blocks field with only the allowed blocks, and render it where the layout permits:
fields: [ hero, { name: 'blocks', type: 'blocks', blocks: [text], maxRows: 5 }, cta,],const { hero, blocks, cta } = page.landing ?? {};
return ( <main className="template-landing"> <Hero {...hero} /> <RenderBlocks blocks={blocks} /> <Cta {...cta} /> </main>);Each template gets its own list, so allowed blocks and limits can differ per template. Add any new block type to the Blocks union and switch in src/blocks/render-blocks.tsx.
Fetching Other Data
Section titled “Fetching Other Data”Templates can be async server components, so they can fetch what they need without changing the routes:
export default async function Contact({ page }: Props) { const payload = await getPayload(); const { businessInfo } = await payload.findGlobal({ slug: 'site-settings', depth: 0, });
// ...render}Switching Templates
Section titled “Switching Templates”Editors can change a page’s template at any time. The page keeps its URL, and content from the old template is hidden but kept, so switching back restores it. Content doesn’t carry over to the new template’s fields.
The Homepage
Section titled “The Homepage”The homepage isn’t a template. Choose it under Site Settings → Reading. The selected page is served at / with whichever template it uses, and its own URL redirects there. If no page is selected, or the selected page is trashed, / returns a 404.
Removing a Template
Section titled “Removing a Template”Move any pages off the template first. render-template.tsx has no fallback, so a page still pointing to a removed template fails to render. Then remove it from both registration files, delete its folder, generate types and create a migration.
Best Practices
Section titled “Best Practices”- Use a template for fixed layouts — if sections alone cover the difference, use the
defaulttemplate with blocks instead. - Mark essential fields
required— this is enforced on publish, not on draft saves. - Use a collection for separate content types — if something needs its own listing or route, like posts, make it a collection.
- Colocate template-only components — keep them in
src/templates/<name>/components/.