Commit d93baefb authored by Fabien Potencier's avatar Fabien Potencier

added a way to render a controller from a template (like in Symfony2)

parent ca797c09
...@@ -62,16 +62,23 @@ The Twig extension provides a ``twig`` service:: ...@@ -62,16 +62,23 @@ The Twig extension provides a ``twig`` service::
This will render a file named ``views/hello.twig``. This will render a file named ``views/hello.twig``.
.. tip:: In any Twig template, the ``app`` variable refers to the Application object.
So you can access any services from within your view. For example to access
``$app['request']->getHost()``, just put this in your template:
The TwigExtension also registers the application as a global .. code-block:: jinja
named ``app``. So you can access any services from within your
view. For example to access ``$app['request']->getHost()``,
just put this in your template:
.. code-block:: jinja
{{ app.request.host }} {{ app.request.host }}
A ``render`` function is also register to help you render another controller
from a template:
.. code-block:: jinja
{{ render('/sidebar') }}
{# or if you are also using UrlGeneratorExtension #}
{{ render(path('sidebar')) }}
For more information, check out the `Twig documentation For more information, check out the `Twig documentation
<http://twig.sensiolabs.org>`_. <http://twig.sensiolabs.org>`_.
<?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\Extension;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Twig extension.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TwigCoreExtension extends \Twig_Extension
{
public function getFunctions()
{
return array(
'render' => new \Twig_Function_Method($this, 'render', array('needs_environment' => true, 'is_safe' => array('html'))),
);
}
public function render(\Twig_Environment $twig, $uri)
{
$globals = $twig->getGlobals();
$request = $globals['app']['request'];
$subRequest = Request::create($uri, 'get', array(), $request->cookies->all(), array(), $request->server->all());
if ($request->getSession()) {
$subRequest->setSession($request->getSession());
}
$response = $globals['app']->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
if (!$response->isSuccessful()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
}
return $response->getContent();
}
public function getName()
{
return 'silex';
}
}
...@@ -39,6 +39,7 @@ class TwigExtension implements ExtensionInterface ...@@ -39,6 +39,7 @@ class TwigExtension implements ExtensionInterface
$twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']); $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
$twig->addGlobal('app', $app); $twig->addGlobal('app', $app);
$twig->addExtension(new TwigCoreExtension());
if (isset($app['symfony_bridges'])) { if (isset($app['symfony_bridges'])) {
if (isset($app['url_generator'])) { if (isset($app['url_generator'])) {
......
...@@ -47,4 +47,29 @@ class TwigExtensionTest extends \PHPUnit_Framework_TestCase ...@@ -47,4 +47,29 @@ class TwigExtensionTest extends \PHPUnit_Framework_TestCase
$response = $app->handle($request); $response = $app->handle($request);
$this->assertEquals('Hello john!', $response->getContent()); $this->assertEquals('Hello john!', $response->getContent());
} }
public function testRenderFunction()
{
$app = new Application();
$app->register(new TwigExtension(), array(
'twig.templates' => array(
'hello' => '{{ render("/foo") }}',
'foo' => 'foo',
),
'twig.class_path' => __DIR__.'/../../../../vendor/twig/lib',
));
$app->get('/hello', function () use ($app) {
return $app['twig']->render('hello');
});
$app->get('/foo', function () use ($app) {
return $app['twig']->render('foo');
});
$request = Request::create('/hello');
$response = $app->handle($request);
$this->assertEquals('foo', $response->getContent());
}
} }
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