Content Composition Builder

Info
This feature is available in Contao 5.7 and later.

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:

// src/Controller/Page/ExamplePageController.php
namespace App\Controller\Page;

use Contao\CoreBundle\ContentComposition\ContentComposition;
use Contao\CoreBundle\DependencyInjection\Attribute\AsPage;
use Contao\PageModel;
use Symfony\Component\HttpFoundation\Response;

#[AsPage]
class ExamplePageController
{
    public function __construct(private readonly ContentComposition $contentComposition)
    {
    }

    public function __invoke(PageModel $pageModel): Response
    {
        $layoutTemplate = $this->contentComposition
            ->createContentCompositionBuilder($pageModel)
            ->buildLayoutTemplate()
        ;

        return $layoutTemplate->getResponse();
    }
}

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;

$layoutTemplate = $this->contentComposition
    ->createContentCompositionBuilder($pageModel)
    ->buildLayoutTemplate()
;

$layoutTemplate->setSlot('main', '<p>Hello World!</p>');

return $layoutTemplate->getResponse();

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:

$layoutTemplate = $this->contentComposition
    ->createContentCompositionBuilder($pageModel)
    ->useCustomLayoutTemplate('page/example')
    ->addArticleToSlot('main')
    ->addElementToSlot('main', 42)
    ->addElementToSlot('header', 7, isContentElement: false)
    ->buildLayoutTemplate()
;

return $layoutTemplate->getResponse();

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.