Commit bdad9831 authored by Avris's avatar Avris

init

parents
tests/_output/*
vendor/*
.idea/*
**/.DS_Store
## Micrus Twig Bridge ##
This is a module for [Micrus framework](http://micrus.avris.it) that allows you to integrate it with [Twig](http://twig.sensiolabs.org/) template engine.
To install this module, run:
composer require avris/micrus-twig
Then open the file `app/Config/modules.yml` and add:
- Avris\Micrus\Assetic\AsseticModule
Usage of Micrus's Twig extension:
<p>Logged in as {{ app.user.username }}</p>
<p>Current language: {{ app.locale }}</p>
{% for flash in app.flashBag %}
<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>
{% 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 %}
### 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, just declare it with a tag `twigExtension`:
myTwigExtension:
class: App\Service\MyTwigExtension
tags: [twigExtension]
{
"name": "avris/micrus-twig",
"type": "library",
"description": "Twig bridge for the Micrus framework",
"keywords": ["twig","template","templating","php","micrus","database"],
"license": "CC-BY",
"homepage": "http://micrus.avris.it",
"authors": [{
"name": "avris",
"email": "andrzej@avris.it",
"homepage": "http://avris.it"
}],
"require": {
"avris/micrus": "^2.0",
"twig/twig": "^1.18"
},
"autoload": {
"psr-4": { "Avris\\Micrus\\Twig\\": "src" }
}
}
<?php
namespace Avris\Micrus\Twig;
class Loader extends \Twig_Loader_Filesystem
{
protected function findTemplate($name)
{
return parent::findTemplate(strpos($name, '.') === false ? $name . '.html.twig' : $name);
}
}
<?php
namespace Avris\Micrus\Twig;
use Avris\Micrus\Module;
class TwigModule implements Module
{
public function extendConfig($env, $rootDir)
{
return [
'services' => [
'templater' => [
'class' => __NAMESPACE__ . '\TwigTemplater',
'parameters' => ['@container', '{@rootDir}/run/cache/{@env}/twig'],
'events' => ['cacheWarmup'],
],
]
];
}
}
<?php
namespace Avris\Micrus\Twig;
use Avris\Micrus\Container;
use Avris\Micrus\Exception\NotFoundException;
use Avris\Micrus\Tool\Cache\CacheWarmupEvent;
use Avris\Micrus\View\Templater;
class TwigTemplater extends Templater
{
/** @var \Twig_Environment */
protected $twig;
public function __construct(Container $container, $cacheDir)
{
parent::__construct($container);
$this->addDir(__DIR__ . '/View');
$this->twig = new \Twig_Environment(
new Loader($this->dirs),
[
'cache' => $cacheDir,
'debug' => $this->env == 'prod' ? false : true,
'strict_variables' => true,
]
);
$this->customizeTwig();
foreach ($container->getByTag('twigExtension') as $extension) {
$this->twig->addExtension($extension);
}
}
protected function customizeTwig()
{
$this->twig->addGlobal('app', $this->app);
$this->twig->addFunction(new \Twig_SimpleFunction(
'route',
[$this, 'route'],
['is_safe' => ['html_attr']]
));
$this->twig->addFunction(new \Twig_SimpleFunction(
'routeExists',
[$this, 'routeExists']
));
$this->twig->addFunction(new \Twig_SimpleFunction(
'asset',
[$this, 'asset'],
['is_safe' => ['html_attr']]
));
$this->twig->addFilter(new \Twig_SimpleFilter(
'l',
[$this, 'localize'],
['is_safe' => ['html']]
));
}
/**
* @param $vars
* @return string
* @throws NotFoundException
*/
public function render($vars)
{
return $this->twig->render($vars['_view'], $vars);
}
public function cacheWarmupEvent(CacheWarmupEvent $event)
{
foreach ($this->dirs as $dir) {
$it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $filename => $file) {
if (!$file->isDir()) {
try {
$this->twig->loadTemplate(substr($filename, strlen($dir) + 1));
} catch (\Twig_Error $e) {}
}
}
}
}
}
{% extends 'layout' %}
{% block content %}
<h3>{{ 'Error %code%'|l({'%code%': errorCode}) }}</h3>
<p><a href="{{ route('home') }}"><span class="fa fa-home"></span> {{ 'Back to homepage'|l }}</a></p>
{% endblock %}
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