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
d93baefb
Commit
d93baefb
authored
Sep 15, 2011
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added a way to render a controller from a template (like in Symfony2)
parent
ca797c09
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
94 additions
and
7 deletions
+94
-7
doc/extensions/twig.rst
doc/extensions/twig.rst
+14
-7
src/Silex/Extension/TwigCoreExtension.php
src/Silex/Extension/TwigCoreExtension.php
+54
-0
src/Silex/Extension/TwigExtension.php
src/Silex/Extension/TwigExtension.php
+1
-0
tests/Silex/Tests/Extension/TwigExtensionTest.php
tests/Silex/Tests/Extension/TwigExtensionTest.php
+25
-0
No files found.
doc/extensions/twig.rst
View file @
d93baefb
...
...
@@ -62,16 +62,23 @@ The Twig extension provides a ``twig`` service::
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
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
.. code-block:: jinja
{{ 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
<http://twig.sensiolabs.org>`_.
src/Silex/Extension/TwigCoreExtension.php
0 → 100644
View file @
d93baefb
<?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'
;
}
}
src/Silex/Extension/TwigExtension.php
View file @
d93baefb
...
...
@@ -39,6 +39,7 @@ class TwigExtension implements ExtensionInterface
$twig
=
new
\Twig_Environment
(
$app
[
'twig.loader'
],
$app
[
'twig.options'
]);
$twig
->
addGlobal
(
'app'
,
$app
);
$twig
->
addExtension
(
new
TwigCoreExtension
());
if
(
isset
(
$app
[
'symfony_bridges'
]))
{
if
(
isset
(
$app
[
'url_generator'
]))
{
...
...
tests/Silex/Tests/Extension/TwigExtensionTest.php
View file @
d93baefb
...
...
@@ -47,4 +47,29 @@ class TwigExtensionTest extends \PHPUnit_Framework_TestCase
$response
=
$app
->
handle
(
$request
);
$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
());
}
}
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