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 ...@@ -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
------------- -------------
......
...@@ -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;
} }
......
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