Skip to content

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.

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
  1. 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, not case-study.

  2. 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.

  3. Register the config

    src/templates/config.ts
    import { about } from './about/config';
    export const pageTemplates: PageTemplate[] = [defaultTemplate, about];
  4. 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.

  5. Generate types

    Terminal window
    ddev pnpm generate:types
  6. Create a migration

    Terminal window
    ddev pnpm payload migrate:create add_about_template

    See Database Migrations for details.

  7. Select the template

    Open a page in the admin panel, and choose About from the Template dropdown.

To give editors limited freedom, add a blocks field with only the allowed blocks, and render it where the layout permits:

src/templates/landing/config.ts
fields: [
hero,
{ name: 'blocks', type: 'blocks', blocks: [text], maxRows: 5 },
cta,
],
src/templates/landing/landing.tsx
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.

Templates can be async server components, so they can fetch what they need without changing the routes:

src/templates/contact/contact.tsx
export default async function Contact({ page }: Props) {
const payload = await getPayload();
const { businessInfo } = await payload.findGlobal({
slug: 'site-settings',
depth: 0,
});
// ...render
}

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 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.

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.

  • Use a template for fixed layouts — if sections alone cover the difference, use the default template 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/.