Content Composition Builder
Info
The ContentCompositionBuilder prepares modern Twig page layouts for rendering in your own page controller.
You can create a builder for the current PageModel via the Contao\CoreBundle\ContentComposition\ContentComposition
service.
Note
You only need to use the builder directly for custom requirements, such as rendering individual slots separately.
Otherwise, extend AbstractPageController and use its
renderPage() method, which handles page rendering for you,
including response context finalization.
Rendering a page layout
By default, the builder uses the page’s assigned layout and its configured content elements, front end modules and
articles. The layout must be of the modern Twig type. Call buildLayoutTemplate() to obtain a LayoutTemplate, then
getResponse() to render it:
Before rendering, you can pass additional Twig variables via $layoutTemplate->set('example', $value).
Replacing a Slot
To completely replace the content configured for a slot, call setSlot()
on the built layout template before rendering;
The content elements, modules and articles originally assigned to main will not be rendered. Other slots keep their
configured content. Slot content is treated as trusted HTML. For dynamic content, pass the rendered output of a Twig
template.
Custom Layout Template
Use useCustomLayoutTemplate() to provide your own template without requiring a page layout from the back end.
The builder then skips the assigned layout and its settings. This way you can add the desired content to the template’s
slots yourself:
This uses templates/page/example.html.twig, which should define the main and header slots.
addArticleToSlot() adds the page’s articles for that slot. addElementToSlot() adds a content element by its
tl_content ID, or a front end module by its tl_module ID when isContentElement is false.