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
020b3138
Commit
020b3138
authored
Sep 15, 2015
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reverted app Twig variable change, introduced a new global one instead
parent
7b095c09
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
43 deletions
+52
-43
doc/changelog.rst
doc/changelog.rst
+1
-1
doc/providers/translation.rst
doc/providers/translation.rst
+2
-9
doc/providers/twig.rst
doc/providers/twig.rst
+42
-30
src/Silex/Provider/TwigServiceProvider.php
src/Silex/Provider/TwigServiceProvider.php
+5
-1
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
+2
-2
No files found.
doc/changelog.rst
View file @
020b3138
...
@@ -5,7 +5,7 @@ Changelog
...
@@ -5,7 +5,7 @@ Changelog
------------------
------------------
* added support for the Symfony VarDumper Component
* added support for the Symfony VarDumper Component
*
[BC BREAK] The app variable exposed in Twig is now an AppVariable instance
*
added a global Twig variable (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/translation.rst
View file @
020b3138
...
@@ -183,15 +183,8 @@ Accessing translations in Twig templates
...
@@ -183,15 +183,8 @@ Accessing translations in Twig templates
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Once loaded, the translation service provider is available from within Twig
Once loaded, the translation service provider is available from within Twig
templates:
templates when using the Twig bridge provided by Symfony (see
:doc:`TwigServiceProvider </providers/twig>`):
.. code-block:: jinja
{{ app.translator.trans('translation_key') }}
Moreover, when using the Twig bridge provided by Symfony (see
:doc:`TwigServiceProvider </providers/twig>`), you will be allowed to translate
strings in the Twig way:
.. code-block:: jinja
.. code-block:: jinja
...
...
doc/providers/twig.rst
View file @
020b3138
...
@@ -51,6 +51,17 @@ Registering
...
@@ -51,6 +51,17 @@ Registering
composer require twig/twig
composer require twig/twig
Usage
-----
The Twig provider provides a ``twig`` service that can render templates::
$app->get('/hello/{name}', function ($name) use ($app) {
return $app['twig']->render('hello.twig', array(
'name' => $name,
));
});
Symfony Components Integration
Symfony Components Integration
------------------------------
------------------------------
...
@@ -62,7 +73,7 @@ some Symfony components and Twig. Add it as a dependency:
...
@@ -62,7 +73,7 @@ some Symfony components and Twig. Add it as a dependency:
composer require symfony/twig-bridge
composer require symfony/twig-bridge
When present, the ``TwigServiceProvider`` will provide you with the following
When present, the ``TwigServiceProvider`` will provide you with the following
additional capabilities
:
additional capabilities
.
* Access to the ``path()`` and ``url()`` functions. You can find more
* Access to the ``path()`` and ``url()`` functions. You can find more
information in the `Symfony Routing documentation
information in the `Symfony Routing documentation
...
@@ -77,53 +88,54 @@ additional capabilities:
...
@@ -77,53 +88,54 @@ additional capabilities:
* Access to the ``absolute_url()`` and ``relative_path()`` Twig functions.
* Access to the ``absolute_url()`` and ``relative_path()`` Twig functions.
* **TranslationServiceProvider**: If you are using the
Translations Support
``TranslationServiceProvider``, you will get the ``trans()`` and
~~~~~~~~~~~~~~~~~~~~
``transchoice()`` functions for translation in Twig templates. You can find
more information in the `Symfony Translation documentation
<http://symfony.com/doc/current/book/translation.html#twig-templates>`_.
* **FormServiceProvider**: If you are using the ``FormServiceProvider``, you
If you are using the ``TranslationServiceProvider``, you will get the
will get a set of helpers for working with forms in templates. You can find
``trans()`` and ``transchoice()`` functions for translation in Twig templates.
more information in the `Symfony Forms reference
You can find more information in the `Symfony Translation documentation
<http://symfony.com/doc/current/reference/forms/twig_reference.html
>`_.
<http://symfony.com/doc/current/book/translation.html#twig-templates
>`_.
* **SecurityServiceProvider**: If you are using the
Form Support
``SecurityServiceProvider``, you will have access to the ``is_granted()``
~~~~~~~~~~~~
function in templates. You can find more information in the `Symfony
Security documentation
<http://symfony.com/doc/current/book/security.html#access-control-in-templates>`_.
Usage
If you are using the ``FormServiceProvider``, you will get a set of helpers for
-----
working with forms in templates. You can find more information in the `Symfony
Forms reference
<http://symfony.com/doc/current/reference/forms/twig_reference.html>`_.
The Twig provider provides a ``twig`` service::
Security Support
~~~~~~~~~~~~~~~~
$app->get('/hello/{name}', function ($name) use ($app) {
If you are using the ``SecurityServiceProvider``, you will have access to the
return $app['twig']->render('hello.twig', array(
``is_granted()`` function in templates. You can find more information in the
'name' => $name,
`Symfony Security documentation
));
<http://symfony.com/doc/current/book/security.html#access-control-in-templates>`
});
_.
This will render a file named ``views/hello.twig``.
Global Variable
~~~~~~~~~~~~~~~
In any Twig template, the ``app`` variable refers to an instance of
When the Twig bridge is available, the ``global`` variable refers to an
`AppVariable <http://api.symfony.com/master/Symfony/Bridge/Twig/AppVariable.html >`_.
instance of
`AppVariable <http://api.symfony.com/master/Symfony/Bridge/Twig/AppVariable.html >`_.
It gives access to the following methods:
It gives access to the following methods:
.. code-block:: jinja
.. code-block:: jinja
{# The current Request #}
{# The current Request #}
{{
app
.request }}
{{
global
.request }}
{# The current User (when security is enabled) #}
{# The current User (when security is enabled) #}
{{
app
.user }}
{{
global
.user }}
{# The current Session #}
{# The current Session #}
{{
app
.session }}
{{
global
.session }}
{# The debug flag #}
{# The debug flag #}
{{ app.debug }}
{{ global.debug }}
Rendering a Controller
~~~~~~~~~~~~~~~~~~~~~~
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 (available when the `HttpFragment Service Provider </providers/http_fragment.rst>`
from a template (available when the `HttpFragment Service Provider </providers/http_fragment.rst>`
...
...
src/Silex/Provider/TwigServiceProvider.php
View file @
020b3138
...
@@ -62,13 +62,17 @@ class TwigServiceProvider implements ServiceProviderInterface
...
@@ -62,13 +62,17 @@ class TwigServiceProvider implements ServiceProviderInterface
);
);
$twig
=
$app
[
'twig.environment_factory'
](
$app
);
$twig
=
$app
[
'twig.environment_factory'
](
$app
);
$twig
->
addGlobal
(
'app'
,
$app
[
'twig.app_variable'
]);
// registered for BC, but should not be used anymore
// deprecated and should probably be removed in Silex 3.0
$twig
->
addGlobal
(
'app'
,
$app
);
if
(
$app
[
'debug'
])
{
if
(
$app
[
'debug'
])
{
$twig
->
addExtension
(
new
\Twig_Extension_Debug
());
$twig
->
addExtension
(
new
\Twig_Extension_Debug
());
}
}
if
(
class_exists
(
'Symfony\Bridge\Twig\Extension\RoutingExtension'
))
{
if
(
class_exists
(
'Symfony\Bridge\Twig\Extension\RoutingExtension'
))
{
$twig
->
addGlobal
(
'global'
,
$app
[
'twig.app_variable'
]);
if
(
isset
(
$app
[
'request_stack'
]))
{
if
(
isset
(
$app
[
'request_stack'
]))
{
$twig
->
addExtension
(
new
HttpFoundationExtension
(
$app
[
'request_stack'
]));
$twig
->
addExtension
(
new
HttpFoundationExtension
(
$app
[
'request_stack'
]));
$twig
->
addExtension
(
new
RoutingExtension
(
$app
[
'url_generator'
]));
$twig
->
addExtension
(
new
RoutingExtension
(
$app
[
'url_generator'
]));
...
...
tests/Silex/Tests/Provider/TwigServiceProviderTest.php
View file @
020b3138
...
@@ -84,13 +84,13 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
...
@@ -84,13 +84,13 @@ 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
test
App
Variable
()
public
function
test
Global
Variable
()
{
{
$app
=
new
Application
();
$app
=
new
Application
();
$app
[
'request_stack'
]
->
push
(
Request
::
create
(
'/?name=Fabien'
));
$app
[
'request_stack'
]
->
push
(
Request
::
create
(
'/?name=Fabien'
));
$app
->
register
(
new
TwigServiceProvider
(),
array
(
$app
->
register
(
new
TwigServiceProvider
(),
array
(
'twig.templates'
=>
array
(
'hello'
=>
'{{
app
.request.get("name") }}'
),
'twig.templates'
=>
array
(
'hello'
=>
'{{
global
.request.get("name") }}'
),
));
));
$this
->
assertEquals
(
'Fabien'
,
$app
[
'twig'
]
->
render
(
'hello'
));
$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