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
72ac7c5a
Commit
72ac7c5a
authored
Jun 19, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
merged the render and stream method of TwigTrait
parent
a439ae7f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
28 deletions
+15
-28
doc/providers/twig.rst
doc/providers/twig.rst
+7
-4
src/Silex/Application/TwigTrait.php
src/Silex/Application/TwigTrait.php
+8
-24
No files found.
doc/providers/twig.rst
View file @
72ac7c5a
...
@@ -128,19 +128,22 @@ Traits
...
@@ -128,19 +128,22 @@ Traits
* **render**: Renders a view with the given parameters and returns a Response
* **render**: Renders a view with the given parameters and returns a Response
object.
object.
* **stream**:: Streams a view and returns a Response.
.. code-block:: php
.. code-block:: php
return $app->render('index.html', ['name': 'Fabien']);
return $app->render('index.html', ['name': 'Fabien']);
return $app->stream('index.html', ['name': 'Fabien']);
$response = new Response();
$response = new Response();
$response->setTtl(10);
$response->setTtl(10);
return $app->render('index.html', ['name': 'Fabien'], $response);
return $app->render('index.html', ['name': 'Fabien'], $response);
.. code-block:: php
// stream a view
use Symfony\Component\HttpFoundation\StreamedResponse;
return $app->render('index.html', ['name': 'Fabien'], new StreamedResponse());
Customization
Customization
-------------
-------------
...
...
src/Silex/Application/TwigTrait.php
View file @
72ac7c5a
...
@@ -24,6 +24,8 @@ trait TwigTrait
...
@@ -24,6 +24,8 @@ trait TwigTrait
/**
/**
* Renders a view and returns a Response.
* Renders a view and returns a Response.
*
*
* To stream a view, pass an instance of StreamedResponse as a third argument.
*
* @param string $view The view name
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param array $parameters An array of parameters to pass to the view
* @param Response $response A Response instance
* @param Response $response A Response instance
...
@@ -36,34 +38,16 @@ trait TwigTrait
...
@@ -36,34 +38,16 @@ trait TwigTrait
$response
=
new
Response
();
$response
=
new
Response
();
}
}
$response
->
setContent
(
$this
->
renderView
(
$view
,
$parameters
));
return
$response
;
}
/**
* Streams a view.
*
* @param string $view The view name
* @param array $parameters An array of parameters to pass to the view
* @param StreamedResponse $response A response instance
*
* @return StreamedResponse A StreamedResponse instance
*/
public
function
stream
(
$view
,
array
$parameters
=
array
(),
StreamedResponse
$response
=
null
)
{
$twig
=
$this
[
'twig'
];
$twig
=
$this
[
'twig'
];
$callback
=
function
()
use
(
$twig
,
$view
,
$parameters
)
{
if
(
$response
instanceof
StreamedResponse
)
{
$
this
[
'twig'
]
->
display
(
$view
,
$parameters
);
$
response
->
setCallback
(
function
()
use
(
$twig
,
$view
,
$parameters
)
{
}
;
$this
[
'twig'
]
->
display
(
$view
,
$parameters
)
;
});
if
(
null
===
$response
)
{
}
else
{
return
new
StreamedResponse
(
$callback
);
$response
->
setContent
(
$twig
->
render
(
$view
,
$parameters
)
);
}
}
$response
->
setCallback
(
$callback
);
return
$response
;
return
$response
;
}
}
...
...
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