Commit 09b0e22f authored by Avris's avatar Avris

v3.0

parent 79d98a5c
## Micrus Twig Bridge ##
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.
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:
......@@ -12,15 +13,14 @@ Then run:
Usage of Micrus's Twig extension:
<p>Logged in as {{ app.user.username }}</p>
<p>Logged in as {{ app.user.identifier }}</p>
<p>Current language: {{ app.locale }}</p>
{% for flash in app.flashBag %}
{% for flash in app.flashBag.all %}
<div class="alert alert-{{ flash.type }}">
<p>{{ flash.message }}</p>
</div>
{% endfor %}
<p>Parameter: {{ app.parameters.paramName }}</p>
<p>Used controller: {{ app.request.matchedRoute.controllerName }}/{{ app.request.matchedRoute.action }}</p>
<p>Used controller: {{ app.request.routeMatch.target }}</p>
{% if routeExists('routeName') %}
<p>
......@@ -33,7 +33,7 @@ Usage of Micrus's Twig extension:
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, just declare it with a tag `twigExtension`:
To register that extension using Micrus's DI Container, just declare it with a tag `twigExtension`:
myTwigExtension:
class: App\Service\MyTwigExtension
......@@ -42,4 +42,4 @@ To register that extension using Micrus's DI, just declare it with a tag `twigEx
### Copyright ###
* **Author:** Andrzej Prusinowski [(Avris.it)](https://avris.it)
* **Licence:** [MIT](https://opensource.org/licenses/MIT)
* **Licence:** [MIT](https://mit.avris.it)
......@@ -11,8 +11,8 @@
"homepage": "https://avris.it"
}],
"require": {
"avris/micrus": "^2.1",
"twig/twig": "^1.18"
"avris/micrus": "^3.0",
"twig/twig": "^1.31"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\Twig\\": "src" }
......
......@@ -11,7 +11,7 @@ class TwigModule implements Module
'services' => [
'templater' => [
'class' => TwigTemplater::class,
'parameters' => ['@container', '{@rootDir}/run/cache/{@env}/twig'],
'params' => ['@container', '{@rootDir}/run/cache/{@env}/twig'],
'events' => ['cacheWarmup'],
],
]
......
<?php
namespace Avris\Micrus\Twig;
use Avris\Micrus\Bootstrap\Container;
use Avris\Micrus\Exception\NotFoundException;
use Avris\Micrus\Bootstrap\ContainerInterface;
use Avris\Micrus\Tool\Cache\CacheWarmupEvent;
use Avris\Micrus\Tool\VarDumper;
use Avris\Micrus\View\Templater;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
class TwigTemplater extends Templater
{
......@@ -56,22 +57,23 @@ class TwigTemplater extends Templater
['is_safe' => ['html_attr']]
));
$this->twig->addFilter(new \Twig_SimpleFilter(
'l',
[$this, 'localize'],
['is_safe' => ['html']]
));
$this->twig->addFunction(new \Twig_SimpleFunction(
'isGranted',
[$this, 'isGranted']
));
if (class_exists('Symfony\Component\VarDumper\Dumper\HtmlDumper')) {
$dumper = new VarDumper();
$cloner = new VarCloner();
$htmlDumper = new HtmlDumper();
$cliDumper = new CliDumper();
$this->twig->addFunction(new \Twig_SimpleFunction(
'dump',
function ($var, $html = true) use ($dumper) { return $dumper->dump($var, $html); },
function ($var, $html = true) use ($cloner, $htmlDumper, $cliDumper) {
$dumper = $html ? $htmlDumper : $cliDumper;
return $dumper->dump($cloner->cloneVar($var), true);
},
['is_safe' => ['html']]
));
}
......@@ -84,8 +86,6 @@ class TwigTemplater extends Templater
*/
public function render($vars)
{
$this->twig->addGlobal('app', $this->buildAppVar());
return $this->twig->render($vars['_view'], $vars);
}
......@@ -99,7 +99,7 @@ class TwigTemplater extends Templater
}
}
public function onCacheWarmup(CacheWarmupEvent $event)
public function onCacheWarmup()
{
foreach ($this->dirs as $dir) {
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment