Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/devtronic/twig-compose

A twig extension to compose templates from multiple child templates.
https://github.com/devtronic/twig-compose

multi-extend php symfony twig twig-extension

Last synced: 18 days ago
JSON representation

A twig extension to compose templates from multiple child templates.

Awesome Lists containing this project

README

        

# ✨ Twig Compose Environment ✨
A twig extension to compose templates from multiple child templates.

## ❓ What does this?
For example you've a base template and N child template which can modify the base Template.
If the templates are fixed, there is no problem you can extend from each other.
Is the number of templates dynamic you can't use extend. In that case you can use this bundle.
Simply call the compose method, pass a base template and a array of sub-templates... and wibbly wobbly you've a template
which contains every content of every child template 🌠

## 📦 Installation
```bash
$ composer require devtronic/twig-compose
```

## ❤ Sponsors
No sponsors at the moment. [Become a sponsor](https://github.com/sponsors/devtronic)

## 🛠 Usage
For details take a look in the tests directory
### ➡ With a your existing Twig_Environment
```php
addExtension(new Devtronic\TwigCompose\ComposeExtension());
$template = ComposeEnvironment::composeStatic($twig, 'base.html.twig', ['pluginA.html.twig', 'pluginB.html.twig']);

echo $template->render([]);
```

### ➡ With the ComposeEnvironment
```php
compose('base.html.twig', ['pluginA.html.twig', 'pluginB.html.twig']);

echo $template->render([]);
```

### ➡ Auto composing templates (Experimental & ComposeEnvironment only)
```php
addPath('/res/theme1', 'Theme1');
$loader->addPath('/res/theme2', 'Theme2');

// If autocompose is enabled, all registered paths will be checked.
// If a file with the the same name (e.g. base.html.twig) exists,
// it will be loaded & composed with the `base.html.twig` inside the main-theme folder
$twig->setAutoCompose(true);
$template = $twig->load('base.html.twig');

echo $template->render([]);
```