Extend your twig templates without inheritance.
composer require braunstetter/template-hooks-bundle
You can use the hook
tag inside your templates now:
{{ hook('app.cp.global-header') }}
Once you inserted this tag somewhere you and any bundles can hook into this by creating a class :
<?php
namespace App\Twig;
use Braunstetter\TemplateHooks\Twig\TemplateHook;
class BreadcrumbsHook extends TemplateHook
{
/**
* @inheritDoc
*/
public function render(): string
{
return $this->templating->render('hooks/breadcrumbs.html.twig', $this->context);
}
public function setTarget(): string|array
{
return 'app.cp.global-header';
// it would be possible to register to multiple hooks
// return ['app.cp.global-header', 'app.cp.global-sidebar'];
}
}
That's it. Your template gets rendered and you can process any logic before rendering it.
With the use of AssetsPushBundle you can write
inside hooks/breadcrumbs.html.twig
:
{% css '/breadcrumbs.css' %}
The css or js will get included inside the head of the page.
For more information follow the official docs.