braunstetter / template-hooks-bundle
Give your bundles a chance to extend your templates.
Installs: 51
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.0
- symfony/framework-bundle: ^4.4|^5.0|^6.0
- symfony/twig-bundle: ^5.3|^6.0
- symfony/yaml: ^5.3|^6.0
Requires (Dev)
README
Extend your twig templates without inheritance.
Installation
composer require braunstetter/template-hooks-bundle
Usage
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.
Ship javascript and css
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.