...
 
Commits (2)
tests/_output/*
vendor/*
.idea/*
/.idea/*
**/.DS_Store
/vendor/*
/tests/_output/*
......@@ -3,41 +3,50 @@
This is a module for [Micrus framework](https://micrus.avris.it) that allows you
to integrate it with [Twig](http://twig.sensiolabs.org/) template engine.
To install this module, open the file `app/Config/modules.yml` and add:
### Installation
- Avris\Micrus\Twig\TwigModule
In your `App\App:registerModules` register the module:
Then run:
yield new \Avris\Micrus\Twig\TwigModule;
then run:
composer require avris/micrus-twig
Usage of Micrus's Twig extension:
### Usage
Just put your `*.twig` templates in the `/templates` directory and render them in your controller.
If you don't specify an extension, `.html.twig` will be assumed:
$this->render('Post/show', ['post' => $post]); // will render Post/show.html.twig
A twig global `app` is added, which gives you access to:
* `app.user` (`null` if not logged in)
* `app.flashBag`
* `app.request`
* `app.routeMatch`
* `app.session`
<p>Logged in as {{ app.user.identifier }}</p>
<p>Current language: {{ app.locale }}</p>
{% for flash in app.flashBag.all %}
<div class="alert alert-{{ flash.type }}">
<p>{{ flash.message }}</p>
</div>
{% endfor %}
<p>Used controller: {{ app.request.routeMatch.target }}</p>
Also some helper functions are provided:
{% if routeExists('routeName') %}
<p>
<a href="route('routeName', {foo: bar})">{{ 'menu.routeName.link'|l }}</a>
<img src="asset('images/filename.png')" alt="{{ 'menu.routeName.alt' }}"/>
</p>
{% endif %}
* `route('route', {params: value})`
* `routeExists('route')`
* `asset('asset.css')`
* `isGranted('ROLE_ADMIN')`
* `canAccess('check', object)`
* `dump(object)`
### Extending Twig ###
### Extending Twig
To create Twig extension, please follow [its documentation](http://twig.sensiolabs.org/doc/advanced.html#creating-an-extension).
To register that extension using Micrus's DI Container, just declare it with a tag `twigExtension`:
Any class extending `Twig\Extension\AbstractExtension` in an autoloaded directory
will be automatically registered as a Twig extension.
To do it manually, just declare it with a tag `twigExtension`:
myTwigExtension:
class: App\Service\MyTwigExtension
tags: [twigExtension]
App\Service\MyTwigExtension:
tags: [twigExtension]
### Copyright ###
......
......@@ -25,8 +25,7 @@ final class TwigEngine implements EngineInterface, EventSubscriberInterface
bool $envAppDebug,
string $cacheDir,
array $twigExtensions
)
{
) {
$this->dirs = $dirs;
$this->twig = new Environment(
new Loader($dirs),
......