From cbdf806cba5dd085d01b9cff501a1a97711d161b Mon Sep 17 00:00:00 2001 From: Guillaume Royer Date: Thu, 1 May 2014 17:36:37 +0200 Subject: [PATCH] [Templating] #9071 array param for EngineInterface::render() We allow an array to be given as param to Symfony/Component/Templating/EngineInterface::render() and Symfony/Bundle/FrameworkBundle/Templating /EngineInterface::renderView(). We modify Symfony\Bridge \Twig\TwigEngine so it accepts an array as param to TwigEngine::render(). We add a test for that. Controller:render() and ::renderView() now accept array as parameter. --- src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php | 1 + src/Symfony/Bridge/Twig/TwigEngine.php | 9 ++++++--- .../Bundle/FrameworkBundle/Controller/Controller.php | 10 +++++----- .../FrameworkBundle/Templating/EngineInterface.php | 2 +- src/Symfony/Component/Templating/EngineInterface.php | 6 +++--- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php b/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php index e7047c354d080..69eccfd2ba3be 100644 --- a/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php +++ b/src/Symfony/Bridge/Twig/Tests/TwigEngineTest.php @@ -51,6 +51,7 @@ public function testRender() { $engine = $this->getTwig(); + $this->assertSame('foo', $engine->render(array('foo', 'index'))); $this->assertSame('foo', $engine->render('index')); $this->assertSame('foo', $engine->render(new TemplateReference('index'))); } diff --git a/src/Symfony/Bridge/Twig/TwigEngine.php b/src/Symfony/Bridge/Twig/TwigEngine.php index 3e3257e7fa0f5..39e8c05fe0426 100644 --- a/src/Symfony/Bridge/Twig/TwigEngine.php +++ b/src/Symfony/Bridge/Twig/TwigEngine.php @@ -109,8 +109,9 @@ public function supports($name) /** * Loads the given template. * - * @param string|TemplateReferenceInterface|\Twig_Template $name A template name or an instance of - * TemplateReferenceInterface or \Twig_Template + * @param string|TemplateReferenceInterface|\Twig_Template|array $name A template name or an instance of + * TemplateReferenceInterface or \Twig_Template or + * an array of thoses. * * @return \Twig_TemplateInterface A \Twig_TemplateInterface instance * @@ -123,7 +124,9 @@ protected function load($name) } try { - return $this->environment->loadTemplate((string) $name); + return $this->environment->resolveTemplate( + is_array($name) ? $name : (string) $name + ); } catch (\Twig_Error_Loader $e) { throw new \InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php index 73f603486b90b..bebea4a5a3182 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php @@ -83,8 +83,8 @@ public function redirect($url, $status = 302) /** * Returns a rendered view. * - * @param string $view The view name - * @param array $parameters An array of parameters to pass to the view + * @param string|array $view The view name or an array of view names. + * @param array $parameters An array of parameters to pass to the view * * @return string The rendered view */ @@ -96,9 +96,9 @@ public function renderView($view, array $parameters = array()) /** * Renders a view. * - * @param string $view The view name - * @param array $parameters An array of parameters to pass to the view - * @param Response $response A response instance + * @param string|array $view The view name or an array of view names. + * @param array $parameters An array of parameters to pass to the view + * @param Response $response A response instance * * @return Response A Response instance */ diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/EngineInterface.php b/src/Symfony/Bundle/FrameworkBundle/Templating/EngineInterface.php index dcf58975176bb..4805d23984783 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/EngineInterface.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/EngineInterface.php @@ -24,7 +24,7 @@ interface EngineInterface extends BaseEngineInterface /** * Renders a view and returns a Response. * - * @param string $view The view name + * @param string|array $view The view name or an array of view names. * @param array $parameters An array of parameters to pass to the view * @param Response $response A Response instance * diff --git a/src/Symfony/Component/Templating/EngineInterface.php b/src/Symfony/Component/Templating/EngineInterface.php index 9302ba3ea1874..3202dcef92d0b 100644 --- a/src/Symfony/Component/Templating/EngineInterface.php +++ b/src/Symfony/Component/Templating/EngineInterface.php @@ -35,8 +35,8 @@ interface EngineInterface /** * Renders a template. * - * @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance - * @param array $parameters An array of parameters to pass to the template + * @param string|TemplateReferenceInterface|array $name A template name or a TemplateReferenceInterface instance or an array of view names. + * @param array $parameters An array of parameters to pass to the template * * @return string The evaluated template as a string * @@ -62,7 +62,7 @@ public function exists($name); /** * Returns true if this class is able to render the given template. * - * @param string|TemplateReferenceInterface $name A template name or a TemplateReferenceInterface instance + * @param string|TemplateReferenceInterface|array $name A template name or a TemplateReferenceInterface instance or an array of thoses. * * @return bool true if this class supports the given template, false otherwise *