Commit 737d4f0f authored by Fabien Potencier's avatar Fabien Potencier

[TwigExtension] added a way to use in-memory templates

Usage:

use Silex\Application;
use Silex\Extension\TwigExtension;

require_once __DIR__.'/silex.phar';

$app = new Application();

$templates = array(
    'hello' => 'Hello {{ name }}',
);

$app->register(new TwigExtension(), array(
    'twig.templates'  => $templates,
    'twig.class_path' => '/path/to//Twig/lib',
));

$app->get('/{name}', function($name) use ($app) {
    return $app['twig']->render('hello', array('name' => $name));
});

$app->run();
parent f11cb433
...@@ -19,7 +19,7 @@ class TwigExtension implements ExtensionInterface ...@@ -19,7 +19,7 @@ class TwigExtension implements ExtensionInterface
public function register(Application $app) public function register(Application $app)
{ {
$app['twig'] = $app->share(function () use ($app) { $app['twig'] = $app->share(function () use ($app) {
$twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']); $twig = new \Twig_Environment($app['twig.loader'], isset($app['twig.options']) ? $app['twig.options'] : array());
$twig->addGlobal('app', $app); $twig->addGlobal('app', $app);
if (isset($app['twig.configure'])) { if (isset($app['twig.configure'])) {
$app['twig.configure']($twig); $app['twig.configure']($twig);
...@@ -29,7 +29,11 @@ class TwigExtension implements ExtensionInterface ...@@ -29,7 +29,11 @@ class TwigExtension implements ExtensionInterface
}); });
$app['twig.loader'] = $app->share(function () use ($app) { $app['twig.loader'] = $app->share(function () use ($app) {
if (isset($app['twig.templates'])) {
return new \Twig_Loader_Array($app['twig.templates']);
} else {
return new \Twig_Loader_Filesystem($app['twig.path']); return new \Twig_Loader_Filesystem($app['twig.path']);
}
}); });
if (isset($app['twig.class_path'])) { if (isset($app['twig.class_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