Commit cda8dce8 authored by Fabien Potencier's avatar Fabien Potencier

added a Twig extension

Usage:

$app = new Application();
$app->register(new TwigExtension(), array(
    'twig.path'    => __DIR__.'/../templates',
    'twig.options' => array('cache' => __DIR__.'/../cache', 'debug' => true),
));

The "app" variable is registered as a global variable, so it is accessible in all templates:

$app['routing'] = new UrlGenerator(...);

{{ app.routing.generate('homepage') }}
parent c6747411
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
class TwigExtension implements ExtensionInterface
{
public function register(Application $app)
{
$app['twig'] = $app->asShared(function () use ($app) {
$twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
$twig->addGlobal('app', $app);
return $twig;
});
$app['twig.loader'] = $app->asShared(function () use ($app) {
return new \Twig_Loader_Filesystem($app['twig.path']);
});
}
}
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