This section covers the core components of SnapWP's template rendering system: RootLayout
, TemplateRenderer
, and EditorBlocksRenderer
, and how they are used together.
The typical flow is as follows:
-
The
RootLayout
provides the overall HTML structure. -
Inside the
RootLayout
, theTemplateRenderer
fetches template data and manages the<head>
section (usingTemplateHead
andTemplateScripts
internally). -
The
TemplateRenderer
'schildren
function uses theEditorBlocksRenderer
to render the blocks.
The following components are used internally by TemplateRenderer
and RootLayout
and are documented here for completeness in case a more advanced user needs to customize them:
TemplateHead
: Manages template-specific stylesheets.TemplateScripts
: Manages template-specific scripts.GlobalHead
: Manages global styles and fonts.
The RootLayout
component provides the basic HTML structure for your Next.js application, including the <html>
, <head>
, and <body>
tags. It also handles the rendering of global styles and fonts.
import { RootLayout } from '@snapwp/next';
export default function Layout( { children }: { children: React.ReactNode } ) {
return <RootLayout>{ children }</RootLayout>;
}
getGlobalStyles
: (Optional) A function that fetches global styles. Defaults toQueryEngine.getGlobalStyles
. Should return an object containingcustomCss
,globalStylesheet
, andrenderedFontFaces
.children
: (Required) The content to be rendered within the layout. This is where theTemplateRenderer
is placed.
The TemplateRenderer
component is the core of SnapWP's templating system. It fetches template data (styles, blocks, scripts) and renders the block content. Use TemplateRenderer
inside RootLayout
to get global styles, custom CSS and fontfaces of the WP FSE theme in use.
import { TemplateRenderer } from '@snapwp/next';
import {
EditorBlocksRenderer,
blocks as blockDefinitions,
} from '@snapwp/blocks';
export default function Page() {
return (
<TemplateRenderer>
{ ( editorBlocks ) => (
// Render function for editor blocks
<EditorBlocksRenderer
editorBlocks={ editorBlocks }
blockDefinitions={ blockDefinitions }
/>
) }
</TemplateRenderer>
);
}
getTemplateData
: (Optional) A function that fetches the template data. Defaults toQueryEngine.getTemplateData
. This function should accept a pathname (string) and return a Promise that resolves to an object containingeditorBlocks
,bodyClasses
,stylesheets
,scripts
, andscriptModules
.children
: (Required) A render function that receives an array ofBlockData
and returns the JSX to render the block content. This is where you would typically use theEditorBlocksRenderer
.
The EditorBlocksRenderer
component is responsible for rendering the actual content blocks. It receives block data and block definitions, and renders the blocks accordingly. The EditorBlocksRenderer
should be used within the TemplateRenderer
to render the block data fetched by TemplateRenderer
.
import { EditorBlocksRenderer } from '@snapwp/blocks';
import {
EditorBlocksRenderer,
blocks as blockDefinitions,
} from '@snapwp/blocks';
export default function Page() {
return (
<TemplateRenderer>
{ ( editorBlocks ) => {
return (
<EditorBlocksRenderer
editorBlocks={ editorBlocks }
blockDefinitions={ blockDefinitions }
/>
);
} }
</TemplateRenderer>
);
}
editorBlocks
: (Optional) An array of block data objects.blockDefinitions
: (Optional) An object mapping block names to React components.