Commit 0d10c5ce authored by Fabien Potencier's avatar Fabien Potencier

changed the app variable in Twig to be an instance of Symfony AppVariable

parent c2a9015e
...@@ -4,6 +4,7 @@ Changelog ...@@ -4,6 +4,7 @@ Changelog
2.0.0 (2015-XX-XX) 2.0.0 (2015-XX-XX)
------------------ ------------------
* [BC BREAK] The app variable exposed in Twig is now an AppVariable instance
* [BC BREAK] CSRF has been moved to a standalone provider (``form.secret`` is not available anymore) * [BC BREAK] CSRF has been moved to a standalone provider (``form.secret`` is not available anymore)
* added support for the Symfony HttpFoundation Twig bridge extension * added support for the Symfony HttpFoundation Twig bridge extension
* added support for the Symfony Asset Component * added support for the Symfony Asset Component
......
...@@ -111,13 +111,23 @@ The Twig provider provides a ``twig`` service:: ...@@ -111,13 +111,23 @@ The Twig provider provides a ``twig`` service::
This will render a file named ``views/hello.twig``. This will render a file named ``views/hello.twig``.
In any Twig template, the ``app`` variable refers to the Application object. In any Twig template, the ``app`` variable refers to an instance of
So you can access any service from within your view. For example to access `AppVariable <http://api.symfony.com/master/Symfony/Bridge/Twig/AppVariable.html >`_.
``$app['request']->getHost()``, just put this in your template: It gives access to the following methods:
.. code-block:: jinja .. code-block:: jinja
{{ app.request.host }} {# The current Request #}
{{ app.request }}
{# The current User (when security is enabled) #}
{{ app.user }}
{# The current Session #}
{{ app.session }}
{# The debug flag #}
{{ app.debug }}
A ``render`` function is also registered to help you render another controller A ``render`` function is also registered to help you render another controller
from a template: from a template:
......
...@@ -13,6 +13,7 @@ namespace Silex\Provider; ...@@ -13,6 +13,7 @@ namespace Silex\Provider;
use Pimple\Container; use Pimple\Container;
use Pimple\ServiceProviderInterface; use Pimple\ServiceProviderInterface;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Bridge\Twig\Extension\AssetExtension; use Symfony\Bridge\Twig\Extension\AssetExtension;
use Symfony\Bridge\Twig\Extension\RoutingExtension; use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension; use Symfony\Bridge\Twig\Extension\TranslationExtension;
...@@ -37,6 +38,17 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -37,6 +38,17 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.path'] = array(); $app['twig.path'] = array();
$app['twig.templates'] = array(); $app['twig.templates'] = array();
$app['twig.app_variable'] = function ($app) {
$var = new AppVariable();
if (isset($app['security.token_storage'])) {
$var->setTokenStorage($app['security.token_storage']);
}
$var->setRequestStack($app['request_stack']);
$var->setDebug($app['debug']);
return $var;
};
$app['twig'] = function ($app) { $app['twig'] = function ($app) {
$app['twig.options'] = array_replace( $app['twig.options'] = array_replace(
array( array(
...@@ -47,7 +59,7 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -47,7 +59,7 @@ class TwigServiceProvider implements ServiceProviderInterface
); );
$twig = $app['twig.environment_factory']($app); $twig = $app['twig.environment_factory']($app);
$twig->addGlobal('app', $app); $twig->addGlobal('app', $app['twig.app_variable']);
if (isset($app['debug']) && $app['debug']) { if (isset($app['debug']) && $app['debug']) {
$twig->addExtension(new \Twig_Extension_Debug()); $twig->addExtension(new \Twig_Extension_Debug());
......
...@@ -81,4 +81,16 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase ...@@ -81,4 +81,16 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/foo.css?1', $app['twig']->render('hello')); $this->assertEquals('/foo.css?1', $app['twig']->render('hello'));
} }
public function testAppVariable()
{
$app = new Application();
$app['request_stack']->push(Request::create('/?name=Fabien'));
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array('hello' => '{{ app.request.get("name") }}'),
));
$this->assertEquals('Fabien', $app['twig']->render('hello'));
}
} }
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