braunstetter/template-hooks-bundle

Give your bundles a chance to extend your templates.

v0.2.2 2021-12-03 18:24 UTC

This package is auto-updated.

Last update: 2024-11-29 06:22:59 UTC


README

Scrutinizer Code Quality Build Status Total Downloads License

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.