Commit 72ac7c5a authored by Fabien Potencier's avatar Fabien Potencier

merged the render and stream method of TwigTrait

parent a439ae7f
......@@ -128,19 +128,22 @@ Traits
* **render**: Renders a view with the given parameters and returns a Response
object.
* **stream**:: Streams a view and returns a Response.
.. code-block:: php
return $app->render('index.html', ['name': 'Fabien']);
return $app->stream('index.html', ['name': 'Fabien']);
$response = new Response();
$response->setTtl(10);
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
-------------
......
......@@ -24,6 +24,8 @@ trait TwigTrait
/**
* 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 array $parameters An array of parameters to pass to the view
* @param Response $response A Response instance
......@@ -36,34 +38,16 @@ trait TwigTrait
$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'];
$callback = function () use ($twig, $view, $parameters) {
$this['twig']->display($view, $parameters);
};
if (null === $response) {
return new StreamedResponse($callback);
if ($response instanceof StreamedResponse) {
$response->setCallback(function () use ($twig, $view, $parameters) {
$this['twig']->display($view, $parameters);
});
} else {
$response->setContent($twig->render($view, $parameters));
}
$response->setCallback($callback);
return $response;
}
......
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