Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
0d10c5ce
Commit
0d10c5ce
authored
Aug 29, 2015
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
changed the app variable in Twig to be an instance of Symfony AppVariable
parent
c2a9015e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
5 deletions
+40
-5
doc/changelog.rst
doc/changelog.rst
+1
-0
doc/providers/twig.rst
doc/providers/twig.rst
+14
-4
src/Silex/Provider/TwigServiceProvider.php
src/Silex/Provider/TwigServiceProvider.php
+13
-1
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
+12
-0
No files found.
doc/changelog.rst
View file @
0d10c5ce
...
@@ -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
...
...
doc/providers/twig.rst
View file @
0d10c5ce
...
@@ -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:
...
...
src/Silex/Provider/TwigServiceProvider.php
View file @
0d10c5ce
...
@@ -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
());
...
...
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
View file @
0d10c5ce
...
@@ -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'
));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment