From 20aa2499c3447e79437544c55c5231140600abe1 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 03:33:24 +0100 Subject: [PATCH 01/18] TemplateInterface::addParameters() method for building up parameters before rendering --- src/Template/AddParametersTrait.php | 51 +++++++++++++++++++++++++++++ src/Template/Plates.php | 15 +++++++++ src/Template/TemplateInterface.php | 10 ++++++ src/Template/Twig.php | 3 +- src/Template/ZendView.php | 4 ++- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Template/AddParametersTrait.php diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php new file mode 100644 index 00000000..ab0693ca --- /dev/null +++ b/src/Template/AddParametersTrait.php @@ -0,0 +1,51 @@ +normalizeParams($params); + } + $this->templateParams[$name] = array_merge($this->templateParams[$name] ?: [], $params, $name); + } + + /** + * Returns merged global, template-specific and given params + * + * @param array $params + * @param string $name + * @return array + */ + protected function mergeParams($params, $name) + { + return array_merge( + $this->templateParams[''] ?: [], + $this->templateParams[$name] ?: [], + $params + ); + } +} \ No newline at end of file diff --git a/src/Template/Plates.php b/src/Template/Plates.php index 340d4787..09a6f527 100644 --- a/src/Template/Plates.php +++ b/src/Template/Plates.php @@ -88,6 +88,21 @@ public function getPaths() return $paths; } + /** + * Add parameters to template + * + * If no template name is given, the parameters will be added to all templates rendered + * + * @param array|object $params + * @param string $name + */ + public function addParameters($params, $name = '') + { + $params = $this->normalizeParams($params); + $this->template->addData($params, $name); + } + + /** * Create a default Plates engine * diff --git a/src/Template/TemplateInterface.php b/src/Template/TemplateInterface.php index 7899b4f6..5e19714e 100644 --- a/src/Template/TemplateInterface.php +++ b/src/Template/TemplateInterface.php @@ -43,4 +43,14 @@ public function addPath($path, $namespace = null); * @return TemplatePath[] */ public function getPaths(); + + /** + * Add parameters to template + * + * If no template name is given, the parameters will be added to all templates rendered + * + * @param array|object $params + * @param string $name + */ + public function addParameters($params, $name = ''); } diff --git a/src/Template/Twig.php b/src/Template/Twig.php index 6cc9c674..2499512a 100644 --- a/src/Template/Twig.php +++ b/src/Template/Twig.php @@ -18,6 +18,7 @@ */ class Twig implements TemplateInterface { + use AddParametersTrait; use ArrayParametersTrait; /** @@ -89,7 +90,7 @@ private function getDefaultLoader() public function render($name, $params = []) { $name = $this->normalizeTemplate($name); - $params = $this->normalizeParams($params); + $params = $this->mergeParams($this->normalizeParams($params), $name); return $this->template->render($name, $params); } diff --git a/src/Template/ZendView.php b/src/Template/ZendView.php index 78e3a5c3..e77db7a4 100644 --- a/src/Template/ZendView.php +++ b/src/Template/ZendView.php @@ -28,6 +28,7 @@ */ class ZendView implements TemplateInterface { + use AddParametersTrait; use ArrayParametersTrait; /** @@ -123,8 +124,9 @@ public function __construct(RendererInterface $renderer = null, $layout = null) */ public function render($name, $params = []) { + $params = $this->mergeParams($this->normalizeParams($params), $name); return $this->renderModel( - $this->createModel($name, $this->normalizeParams($params)), + $this->createModel($name, $params), $this->renderer ); } From ff7f176eb39d473c6892b4817617a146520c8a54 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 21:47:54 +0100 Subject: [PATCH 02/18] Fixed failing tests --- src/Template/AddParametersTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index ab0693ca..5d837458 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -43,8 +43,8 @@ public function addParameters($params, $name = '') protected function mergeParams($params, $name) { return array_merge( - $this->templateParams[''] ?: [], - $this->templateParams[$name] ?: [], + isset($this->templateParams['']) ? $this->templateParams[''] : [], + isset($this->templateParams[$name]) ? $this->templateParams[$name] : [], $params ); } From 52f7f18e285005ed54f129aad907e599345a1bf7 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:30:15 +0100 Subject: [PATCH 03/18] Fix for adding shared params to Plates. Default for template name now null, not empty string. --- src/Template/AddParametersTrait.php | 6 ++++-- src/Template/Plates.php | 2 +- src/Template/TemplateInterface.php | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index 5d837458..edb98244 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -25,12 +25,14 @@ trait AddParametersTrait * @param array|object $params * @param string $name */ - public function addParameters($params, $name = '') + public function addParameters($params, $name = null) { if (method_exists($this, 'normalizeParams')) { $params = $this->normalizeParams($params); } - $this->templateParams[$name] = array_merge($this->templateParams[$name] ?: [], $params, $name); + $name = (string) $name; + $existing = isset($this->templateParams[$name]) ? $this->templateParams[$name] : null; + $this->templateParams[$name] = array_merge($existing, $params, $name); } /** diff --git a/src/Template/Plates.php b/src/Template/Plates.php index 09a6f527..4082188b 100644 --- a/src/Template/Plates.php +++ b/src/Template/Plates.php @@ -96,7 +96,7 @@ public function getPaths() * @param array|object $params * @param string $name */ - public function addParameters($params, $name = '') + public function addParameters($params, $name = null) { $params = $this->normalizeParams($params); $this->template->addData($params, $name); diff --git a/src/Template/TemplateInterface.php b/src/Template/TemplateInterface.php index 5e19714e..221d1c7e 100644 --- a/src/Template/TemplateInterface.php +++ b/src/Template/TemplateInterface.php @@ -50,7 +50,7 @@ public function getPaths(); * If no template name is given, the parameters will be added to all templates rendered * * @param array|object $params - * @param string $name + * @param string|null $name */ - public function addParameters($params, $name = ''); + public function addParameters($params, $name = null); } From 97a808767a8d37d432b88dc93f65ad355cd1ab2c Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:30:48 +0100 Subject: [PATCH 04/18] Fix for adding shared params to Plates. Default for template name now null, not empty string. --- src/Template/AddParametersTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index edb98244..aeb32c17 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -23,7 +23,7 @@ trait AddParametersTrait * If no template name is given, the parameters will be added to all templates rendered * * @param array|object $params - * @param string $name + * @param string|null $name */ public function addParameters($params, $name = null) { From b246c919a8739c21319a985aa593114355bae985 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:31:22 +0100 Subject: [PATCH 05/18] CS fix --- src/Template/AddParametersTrait.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index aeb32c17..7073a987 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -9,7 +9,6 @@ namespace Zend\Expressive\Template; - trait AddParametersTrait { /** @@ -50,4 +49,4 @@ protected function mergeParams($params, $name) $params ); } -} \ No newline at end of file +} From 8cd7645077b82ebd7b48be0e4a90abf022ef8892 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:36:52 +0100 Subject: [PATCH 06/18] Added Plates tests --- test/Template/PlatesTest.php | 70 ++++++++++++++++++++++++++++ test/Template/TestAsset/plates-2.php | 3 ++ 2 files changed, 73 insertions(+) create mode 100644 test/Template/TestAsset/plates-2.php diff --git a/test/Template/PlatesTest.php b/test/Template/PlatesTest.php index 3a7e320a..deac833c 100644 --- a/test/Template/PlatesTest.php +++ b/test/Template/PlatesTest.php @@ -174,4 +174,74 @@ public function testProperlyResolvesNamespacedTemplate() $this->assertSame($expected, $test); } + + public function testAddParameterToOneTemplate() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $template->addParameters(['name' => $name], 'plates'); + $result = $template->render('plates'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + + // @fixme hack to work around https://github.com/thephpleague/plates/issues/60, remove if ever merged + set_error_handler(function ($error, $message) { + $this->assertContains('Undefined variable: name', $message); + return true; + }, E_NOTICE); + $template->render('plates-2'); + restore_error_handler(); + + $content= str_replace('e($name)?>', '', $content); + $this->assertEquals($content, $result); + } + + public function testAddSharedParameters() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $template->addParameters(['name' => $name]); + $result = $template->render('plates'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + $result = $template->render('plates-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersPerTemplate() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $name2 = 'Saucers'; + $template->addParameters(['name' => $name]); + $template->addParameters(['name' => $name2], 'plates-2'); + $result = $template->render('plates'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + $result = $template->render('plates-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php'); + $content= str_replace('e($name)?>', $name2, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersAtRender() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $name2 = 'Saucers'; + $template->addParameters(['name' => $name]); + $result = $template->render('plates', ['name' => $name2]); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name2, $content); + $this->assertEquals($content, $result); + } } diff --git a/test/Template/TestAsset/plates-2.php b/test/Template/TestAsset/plates-2.php new file mode 100644 index 00000000..571e5569 --- /dev/null +++ b/test/Template/TestAsset/plates-2.php @@ -0,0 +1,3 @@ +

This is a second template file for Plates

+ +

You are using e($name)?>!

From d30bbbd570758d7a3204c1c4897285bcc4f74072 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 03:33:24 +0100 Subject: [PATCH 07/18] TemplateInterface::addParameters() method for building up parameters before rendering --- src/Template/AddParametersTrait.php | 51 ++++++++++++++++++++++ src/Template/PlatesRenderer.php | 15 +++++++ src/Template/TemplateRendererInterface.php | 10 +++++ src/Template/TwigRenderer.php | 3 +- src/Template/ZendViewRenderer.php | 4 +- 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/Template/AddParametersTrait.php diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php new file mode 100644 index 00000000..ab0693ca --- /dev/null +++ b/src/Template/AddParametersTrait.php @@ -0,0 +1,51 @@ +normalizeParams($params); + } + $this->templateParams[$name] = array_merge($this->templateParams[$name] ?: [], $params, $name); + } + + /** + * Returns merged global, template-specific and given params + * + * @param array $params + * @param string $name + * @return array + */ + protected function mergeParams($params, $name) + { + return array_merge( + $this->templateParams[''] ?: [], + $this->templateParams[$name] ?: [], + $params + ); + } +} \ No newline at end of file diff --git a/src/Template/PlatesRenderer.php b/src/Template/PlatesRenderer.php index 3f800cbe..450e6334 100644 --- a/src/Template/PlatesRenderer.php +++ b/src/Template/PlatesRenderer.php @@ -88,6 +88,21 @@ public function getPaths() return $paths; } + /** + * Add parameters to template + * + * If no template name is given, the parameters will be added to all templates rendered + * + * @param array|object $params + * @param string $name + */ + public function addParameters($params, $name = '') + { + $params = $this->normalizeParams($params); + $this->template->addData($params, $name); + } + + /** * Create a default Plates engine * diff --git a/src/Template/TemplateRendererInterface.php b/src/Template/TemplateRendererInterface.php index a98752b5..1b37d110 100644 --- a/src/Template/TemplateRendererInterface.php +++ b/src/Template/TemplateRendererInterface.php @@ -43,4 +43,14 @@ public function addPath($path, $namespace = null); * @return TemplatePath[] */ public function getPaths(); + + /** + * Add parameters to template + * + * If no template name is given, the parameters will be added to all templates rendered + * + * @param array|object $params + * @param string $name + */ + public function addParameters($params, $name = ''); } diff --git a/src/Template/TwigRenderer.php b/src/Template/TwigRenderer.php index 4e0350dc..2ea93d95 100644 --- a/src/Template/TwigRenderer.php +++ b/src/Template/TwigRenderer.php @@ -18,6 +18,7 @@ */ class TwigRenderer implements TemplateRendererInterface { + use AddParametersTrait; use ArrayParametersTrait; /** @@ -90,7 +91,7 @@ private function getDefaultLoader() public function render($name, $params = []) { $name = $this->normalizeTemplate($name); - $params = $this->normalizeParams($params); + $params = $this->mergeParams($this->normalizeParams($params), $name); return $this->template->render($name, $params); } diff --git a/src/Template/ZendViewRenderer.php b/src/Template/ZendViewRenderer.php index 6b030c4b..8b1b3c71 100644 --- a/src/Template/ZendViewRenderer.php +++ b/src/Template/ZendViewRenderer.php @@ -28,6 +28,7 @@ */ class ZendViewRenderer implements TemplateRendererInterface { + use AddParametersTrait; use ArrayParametersTrait; /** @@ -118,8 +119,9 @@ public function __construct(RendererInterface $renderer = null, $layout = null) */ public function render($name, $params = []) { + $params = $this->mergeParams($this->normalizeParams($params), $name); return $this->renderModel( - $this->createModel($name, $this->normalizeParams($params)), + $this->createModel($name, $params), $this->renderer ); } From 3e750a426265da9376908aecf0a42a8479d58b38 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 21:47:54 +0100 Subject: [PATCH 08/18] Fixed failing tests --- src/Template/AddParametersTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index ab0693ca..5d837458 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -43,8 +43,8 @@ public function addParameters($params, $name = '') protected function mergeParams($params, $name) { return array_merge( - $this->templateParams[''] ?: [], - $this->templateParams[$name] ?: [], + isset($this->templateParams['']) ? $this->templateParams[''] : [], + isset($this->templateParams[$name]) ? $this->templateParams[$name] : [], $params ); } From 561855f076385c4d2522083c379b31c357b80cdc Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:30:15 +0100 Subject: [PATCH 09/18] Fix for adding shared params to Plates. Default for template name now null, not empty string. --- src/Template/AddParametersTrait.php | 6 ++++-- src/Template/PlatesRenderer.php | 2 +- src/Template/TemplateRendererInterface.php | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index 5d837458..edb98244 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -25,12 +25,14 @@ trait AddParametersTrait * @param array|object $params * @param string $name */ - public function addParameters($params, $name = '') + public function addParameters($params, $name = null) { if (method_exists($this, 'normalizeParams')) { $params = $this->normalizeParams($params); } - $this->templateParams[$name] = array_merge($this->templateParams[$name] ?: [], $params, $name); + $name = (string) $name; + $existing = isset($this->templateParams[$name]) ? $this->templateParams[$name] : null; + $this->templateParams[$name] = array_merge($existing, $params, $name); } /** diff --git a/src/Template/PlatesRenderer.php b/src/Template/PlatesRenderer.php index 450e6334..0bd1099d 100644 --- a/src/Template/PlatesRenderer.php +++ b/src/Template/PlatesRenderer.php @@ -96,7 +96,7 @@ public function getPaths() * @param array|object $params * @param string $name */ - public function addParameters($params, $name = '') + public function addParameters($params, $name = null) { $params = $this->normalizeParams($params); $this->template->addData($params, $name); diff --git a/src/Template/TemplateRendererInterface.php b/src/Template/TemplateRendererInterface.php index 1b37d110..5a117d93 100644 --- a/src/Template/TemplateRendererInterface.php +++ b/src/Template/TemplateRendererInterface.php @@ -50,7 +50,7 @@ public function getPaths(); * If no template name is given, the parameters will be added to all templates rendered * * @param array|object $params - * @param string $name + * @param string|null $name */ - public function addParameters($params, $name = ''); + public function addParameters($params, $name = null); } From cd52672add15b5547e011929f0b3c01c7ae89968 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:30:48 +0100 Subject: [PATCH 10/18] Fix for adding shared params to Plates. Default for template name now null, not empty string. --- src/Template/AddParametersTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index edb98244..aeb32c17 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -23,7 +23,7 @@ trait AddParametersTrait * If no template name is given, the parameters will be added to all templates rendered * * @param array|object $params - * @param string $name + * @param string|null $name */ public function addParameters($params, $name = null) { From ddcf66267cee62aac90b1c058cdcb503b1e54c05 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:31:22 +0100 Subject: [PATCH 11/18] CS fix --- src/Template/AddParametersTrait.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php index aeb32c17..7073a987 100644 --- a/src/Template/AddParametersTrait.php +++ b/src/Template/AddParametersTrait.php @@ -9,7 +9,6 @@ namespace Zend\Expressive\Template; - trait AddParametersTrait { /** @@ -50,4 +49,4 @@ protected function mergeParams($params, $name) $params ); } -} \ No newline at end of file +} From 7d8334f102b373966e730d10594d7a39300fbf7c Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Tue, 29 Sep 2015 23:36:52 +0100 Subject: [PATCH 12/18] Added Plates tests --- test/Template/PlatesRendererTest.php | 70 ++++++++++++++++++++++++++++ test/Template/TestAsset/plates-2.php | 3 ++ 2 files changed, 73 insertions(+) create mode 100644 test/Template/TestAsset/plates-2.php diff --git a/test/Template/PlatesRendererTest.php b/test/Template/PlatesRendererTest.php index c36bcac7..283db64c 100644 --- a/test/Template/PlatesRendererTest.php +++ b/test/Template/PlatesRendererTest.php @@ -184,4 +184,74 @@ public function testProperlyResolvesNamespacedTemplate() $this->assertSame($expected, $test); } + + public function testAddParameterToOneTemplate() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $template->addParameters(['name' => $name], 'plates'); + $result = $template->render('plates'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + + // @fixme hack to work around https://github.com/thephpleague/plates/issues/60, remove if ever merged + set_error_handler(function ($error, $message) { + $this->assertContains('Undefined variable: name', $message); + return true; + }, E_NOTICE); + $template->render('plates-2'); + restore_error_handler(); + + $content= str_replace('e($name)?>', '', $content); + $this->assertEquals($content, $result); + } + + public function testAddSharedParameters() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $template->addParameters(['name' => $name]); + $result = $template->render('plates'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + $result = $template->render('plates-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersPerTemplate() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $name2 = 'Saucers'; + $template->addParameters(['name' => $name]); + $template->addParameters(['name' => $name2], 'plates-2'); + $result = $template->render('plates'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name, $content); + $this->assertEquals($content, $result); + $result = $template->render('plates-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php'); + $content= str_replace('e($name)?>', $name2, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersAtRender() + { + $template = new PlatesTemplate(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Plates'; + $name2 = 'Saucers'; + $template->addParameters(['name' => $name]); + $result = $template->render('plates', ['name' => $name2]); + $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); + $content= str_replace('e($name)?>', $name2, $content); + $this->assertEquals($content, $result); + } } diff --git a/test/Template/TestAsset/plates-2.php b/test/Template/TestAsset/plates-2.php new file mode 100644 index 00000000..571e5569 --- /dev/null +++ b/test/Template/TestAsset/plates-2.php @@ -0,0 +1,3 @@ +

This is a second template file for Plates

+ +

You are using e($name)?>!

From 7cdab23ba5c0447cb98d9776a9905499905a1bc6 Mon Sep 17 00:00:00 2001 From: Matt Kynaston Date: Sat, 10 Oct 2015 15:40:45 +0100 Subject: [PATCH 13/18] Changed PlatesTemplate to PlatesRenderer in tests --- test/Template/PlatesRendererTest.php | 40 ++++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/test/Template/PlatesRendererTest.php b/test/Template/PlatesRendererTest.php index 283db64c..fa80ce5b 100644 --- a/test/Template/PlatesRendererTest.php +++ b/test/Template/PlatesRendererTest.php @@ -187,11 +187,11 @@ public function testProperlyResolvesNamespacedTemplate() public function testAddParameterToOneTemplate() { - $template = new PlatesTemplate(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new PlatesRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; - $template->addParameters(['name' => $name], 'plates'); - $result = $template->render('plates'); + $renderer->addParameters(['name' => $name], 'plates'); + $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); $this->assertEquals($content, $result); @@ -201,7 +201,7 @@ public function testAddParameterToOneTemplate() $this->assertContains('Undefined variable: name', $message); return true; }, E_NOTICE); - $template->render('plates-2'); + $renderer->render('plates-2'); restore_error_handler(); $content= str_replace('e($name)?>', '', $content); @@ -210,15 +210,15 @@ public function testAddParameterToOneTemplate() public function testAddSharedParameters() { - $template = new PlatesTemplate(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new PlatesRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; - $template->addParameters(['name' => $name]); - $result = $template->render('plates'); + $renderer->addParameters(['name' => $name]); + $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); $this->assertEquals($content, $result); - $result = $template->render('plates-2'); + $result = $renderer->render('plates-2'); $content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php'); $content= str_replace('e($name)?>', $name, $content); $this->assertEquals($content, $result); @@ -226,17 +226,17 @@ public function testAddSharedParameters() public function testOverrideSharedParametersPerTemplate() { - $template = new PlatesTemplate(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new PlatesRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; $name2 = 'Saucers'; - $template->addParameters(['name' => $name]); - $template->addParameters(['name' => $name2], 'plates-2'); - $result = $template->render('plates'); + $renderer->addParameters(['name' => $name]); + $renderer->addParameters(['name' => $name2], 'plates-2'); + $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); $this->assertEquals($content, $result); - $result = $template->render('plates-2'); + $result = $renderer->render('plates-2'); $content = file_get_contents(__DIR__ . '/TestAsset/plates-2.php'); $content= str_replace('e($name)?>', $name2, $content); $this->assertEquals($content, $result); @@ -244,12 +244,12 @@ public function testOverrideSharedParametersPerTemplate() public function testOverrideSharedParametersAtRender() { - $template = new PlatesTemplate(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new PlatesRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; $name2 = 'Saucers'; - $template->addParameters(['name' => $name]); - $result = $template->render('plates', ['name' => $name2]); + $renderer->addParameters(['name' => $name]); + $result = $renderer->render('plates', ['name' => $name2]); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name2, $content); $this->assertEquals($content, $result); From 996410733b629fd624e607ec52e457a95d79f189 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sat, 10 Oct 2015 09:49:16 -0500 Subject: [PATCH 14/18] Incorporated feedback for #143 - Adds the method `addDefaultParam($templateName, $key, $value)` to `TemplateRendererInterface`; define a TEMPLATE_ALL constant. Replaces `addParameters()`. - Renames `AddParametersTrait` to `DefaultParamsTrait` - Renames `$templateParams` to `$defaultParams` - Renames `addParameters()` to `addDefaultParam()` - Updates PlatesRenderer to the interface changes. - Modifies Twig and ZendView renderers to use the new trait. - Wrote tests for the behavior for each renderer. --- composer.json | 1 + src/Template/AddParametersTrait.php | 52 ------------- src/Template/DefaultParamsTrait.php | 86 ++++++++++++++++++++++ src/Template/PlatesRenderer.php | 33 +++++++-- src/Template/TemplateRendererInterface.php | 26 +++++-- src/Template/TwigRenderer.php | 10 ++- src/Template/ZendViewRenderer.php | 4 +- test/Template/PlatesRendererTest.php | 10 +-- test/Template/TestAsset/twig-2.html | 3 + test/Template/TestAsset/zendview-2.phtml | 3 + test/Template/TwigRendererTest.php | 62 ++++++++++++++++ test/Template/ZendViewRendererTest.php | 62 ++++++++++++++++ 12 files changed, 279 insertions(+), 73 deletions(-) delete mode 100644 src/Template/AddParametersTrait.php create mode 100644 src/Template/DefaultParamsTrait.php create mode 100644 test/Template/TestAsset/twig-2.html create mode 100644 test/Template/TestAsset/zendview-2.phtml diff --git a/composer.json b/composer.json index 99db9d27..78379cc7 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,7 @@ "container-interop/container-interop": "^1.1", "psr/http-message": "^1.0", "zendframework/zend-diactoros": "^1.1", + "zendframework/zend-stdlib": "^2.7", "zendframework/zend-stratigility": "^1.1" }, "require-dev": { diff --git a/src/Template/AddParametersTrait.php b/src/Template/AddParametersTrait.php deleted file mode 100644 index 7073a987..00000000 --- a/src/Template/AddParametersTrait.php +++ /dev/null @@ -1,52 +0,0 @@ -normalizeParams($params); - } - $name = (string) $name; - $existing = isset($this->templateParams[$name]) ? $this->templateParams[$name] : null; - $this->templateParams[$name] = array_merge($existing, $params, $name); - } - - /** - * Returns merged global, template-specific and given params - * - * @param array $params - * @param string $name - * @return array - */ - protected function mergeParams($params, $name) - { - return array_merge( - isset($this->templateParams['']) ? $this->templateParams[''] : [], - isset($this->templateParams[$name]) ? $this->templateParams[$name] : [], - $params - ); - } -} diff --git a/src/Template/DefaultParamsTrait.php b/src/Template/DefaultParamsTrait.php new file mode 100644 index 00000000..d0c08a0d --- /dev/null +++ b/src/Template/DefaultParamsTrait.php @@ -0,0 +1,86 @@ +defaultParams[$templateName])) { + $this->defaultParams[$templateName] = []; + } + + $this->defaultParams[$templateName][$param] = $value; + } + + /** + * Returns merged global, template-specific and given params + * + * @param string $template + * @param array $params + * @return array + */ + private function mergeParams($template, array $params) + { + $globalDefaults = isset($this->defaultParams[TemplateRendererInterface::TEMPLATE_ALL]) + ? $this->defaultParams[TemplateRendererInterface::TEMPLATE_ALL] + : []; + + $templateDefaults = isset($this->defaultParams[$template]) + ? $this->defaultParams[$template] + : []; + + $defaults = ArrayUtils::merge($globalDefaults, $templateDefaults); + + return ArrayUtils::merge($defaults, $params); + } +} diff --git a/src/Template/PlatesRenderer.php b/src/Template/PlatesRenderer.php index 0bd1099d..b2e0b3b5 100644 --- a/src/Template/PlatesRenderer.php +++ b/src/Template/PlatesRenderer.php @@ -11,6 +11,7 @@ use League\Plates\Engine; use ReflectionProperty; +use Zend\Expressive\Exception; /** * Template implementation bridging league/plates @@ -89,17 +90,35 @@ public function getPaths() } /** - * Add parameters to template + * {@inheritDoc} * - * If no template name is given, the parameters will be added to all templates rendered + * Proxies to the Plate Engine's `addData()` method. * - * @param array|object $params - * @param string $name + * {@inheritDoc} */ - public function addParameters($params, $name = null) + public function addDefaultParam($templateName, $param, $value) { - $params = $this->normalizeParams($params); - $this->template->addData($params, $name); + if (! is_string($templateName) || empty($templateName)) { + throw new Exception\InvalidArgumentException(sprintf( + '$templateName must be a non-empty string; received %s', + (is_object($templateName) ? get_class($templateName) : gettype($templateName)) + )); + } + + if (! is_string($param) || empty($param)) { + throw new Exception\InvalidArgumentException(sprintf( + '$param must be a non-empty string; received %s', + (is_object($param) ? get_class($param) : gettype($param)) + )); + } + + $params = [$param => $value]; + + if ($templateName === self::TEMPLATE_ALL) { + $templateName = null; + } + + $this->template->addData($params, $templateName); } diff --git a/src/Template/TemplateRendererInterface.php b/src/Template/TemplateRendererInterface.php index 5a117d93..68e536c3 100644 --- a/src/Template/TemplateRendererInterface.php +++ b/src/Template/TemplateRendererInterface.php @@ -14,6 +14,11 @@ */ interface TemplateRendererInterface { + /** + * @const string Value indicating all templates; used with `addDefaultParam()`. + */ + const TEMPLATE_ALL = '*'; + /** * Render a template, optionally with parameters. * @@ -45,12 +50,23 @@ public function addPath($path, $namespace = null); public function getPaths(); /** - * Add parameters to template + * Add a default parameter to use with a template. * - * If no template name is given, the parameters will be added to all templates rendered + * Use this method to provide a default parameter to use when a template is + * rendered. The parameter may be overridden by providing it when calling + * `render()`, or by calling this method again with a null value. * - * @param array|object $params - * @param string|null $name + * The parameter will be specific to the template name provided. To make + * the parameter available to any template, pass the TEMPLATE_ALL constant + * for the template name. + * + * If the default parameter existed previously, subsequent invocations with + * the same template name and parameter name will overwrite. + * + * @param string $templateName Name of template to which the param applies; + * use TEMPLATE_ALL to apply to all templates. + * @param string $param Param name. + * @param mixed $value */ - public function addParameters($params, $name = null); + public function addDefaultParam($templateName, $param, $value); } diff --git a/src/Template/TwigRenderer.php b/src/Template/TwigRenderer.php index 2ea93d95..0ff0e2e5 100644 --- a/src/Template/TwigRenderer.php +++ b/src/Template/TwigRenderer.php @@ -18,8 +18,8 @@ */ class TwigRenderer implements TemplateRendererInterface { - use AddParametersTrait; use ArrayParametersTrait; + use DefaultParamsTrait; /** * @var string @@ -90,8 +90,14 @@ private function getDefaultLoader() */ public function render($name, $params = []) { + // Merge parameters based on requested template name + $params = $this->mergeParams($name, $this->normalizeParams($params)); + $name = $this->normalizeTemplate($name); - $params = $this->mergeParams($this->normalizeParams($params), $name); + + // Merge parameters based on normalized template name + $params = $this->mergeParams($name, $params); + return $this->template->render($name, $params); } diff --git a/src/Template/ZendViewRenderer.php b/src/Template/ZendViewRenderer.php index 8b1b3c71..a02808f7 100644 --- a/src/Template/ZendViewRenderer.php +++ b/src/Template/ZendViewRenderer.php @@ -28,8 +28,8 @@ */ class ZendViewRenderer implements TemplateRendererInterface { - use AddParametersTrait; use ArrayParametersTrait; + use DefaultParamsTrait; /** * @var ViewModel @@ -119,7 +119,7 @@ public function __construct(RendererInterface $renderer = null, $layout = null) */ public function render($name, $params = []) { - $params = $this->mergeParams($this->normalizeParams($params), $name); + $params = $this->mergeParams($name, $this->normalizeParams($params)); return $this->renderModel( $this->createModel($name, $params), $this->renderer diff --git a/test/Template/PlatesRendererTest.php b/test/Template/PlatesRendererTest.php index fa80ce5b..6d52b33a 100644 --- a/test/Template/PlatesRendererTest.php +++ b/test/Template/PlatesRendererTest.php @@ -190,7 +190,7 @@ public function testAddParameterToOneTemplate() $renderer = new PlatesRenderer(); $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; - $renderer->addParameters(['name' => $name], 'plates'); + $renderer->addDefaultParam('plates', 'name', $name); $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); @@ -213,7 +213,7 @@ public function testAddSharedParameters() $renderer = new PlatesRenderer(); $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; - $renderer->addParameters(['name' => $name]); + $renderer->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); @@ -230,8 +230,8 @@ public function testOverrideSharedParametersPerTemplate() $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; $name2 = 'Saucers'; - $renderer->addParameters(['name' => $name]); - $renderer->addParameters(['name' => $name2], 'plates-2'); + $renderer->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $renderer->addDefaultParam('plates-2', 'name', $name2); $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); @@ -248,7 +248,7 @@ public function testOverrideSharedParametersAtRender() $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; $name2 = 'Saucers'; - $renderer->addParameters(['name' => $name]); + $renderer->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); $result = $renderer->render('plates', ['name' => $name2]); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name2, $content); diff --git a/test/Template/TestAsset/twig-2.html b/test/Template/TestAsset/twig-2.html new file mode 100644 index 00000000..68803930 --- /dev/null +++ b/test/Template/TestAsset/twig-2.html @@ -0,0 +1,3 @@ +

This is a second template file for Twig

+ +

You are using {{ name }}!

diff --git a/test/Template/TestAsset/zendview-2.phtml b/test/Template/TestAsset/zendview-2.phtml new file mode 100644 index 00000000..56d36182 --- /dev/null +++ b/test/Template/TestAsset/zendview-2.phtml @@ -0,0 +1,3 @@ +

This is a second template file for ZendView

+ +

You are using !

diff --git a/test/Template/TwigRendererTest.php b/test/Template/TwigRendererTest.php index 0081c2dc..a9baee1a 100644 --- a/test/Template/TwigRendererTest.php +++ b/test/Template/TwigRendererTest.php @@ -175,4 +175,66 @@ public function testResolvesNamespacedTemplateWithSuffix() $this->assertSame($expected, $test); } + + public function testAddParameterToOneTemplate() + { + $template = new TwigRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Twig'; + $template->addDefaultParam('twig', 'name', $name); + $result = $template->render('twig'); + + $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); + $content = str_replace('{{ name }}', $name, $content); + $this->assertEquals($content, $result); + } + + public function testAddSharedParameters() + { + $template = new TwigRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Twig'; + $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $result = $template->render('twig'); + $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); + $content = str_replace('{{ name }}', $name, $content); + $this->assertEquals($content, $result); + + $result = $template->render('twig-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/twig-2.html'); + $content = str_replace('{{ name }}', $name, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersPerTemplate() + { + $template = new TwigRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Twig'; + $name2 = 'Template'; + $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $template->addDefaultParam('twig-2', 'name', $name2); + $result = $template->render('twig'); + $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); + $content = str_replace('{{ name }}', $name, $content); + $this->assertEquals($content, $result); + + $result = $template->render('twig-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/twig-2.html'); + $content = str_replace('{{ name }}', $name2, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersAtRender() + { + $template = new TwigRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Twig'; + $name2 = 'Template'; + $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $result = $template->render('twig', ['name' => $name2]); + $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); + $content = str_replace('{{ name }}', $name2, $content); + $this->assertEquals($content, $result); + } } diff --git a/test/Template/ZendViewRendererTest.php b/test/Template/ZendViewRendererTest.php index 01b6eb32..7ce3439f 100644 --- a/test/Template/ZendViewRendererTest.php +++ b/test/Template/ZendViewRendererTest.php @@ -250,4 +250,66 @@ public function testProperlyResolvesNamespacedTemplate() $this->assertSame($expected, $test); } + + public function testAddParameterToOneTemplate() + { + $template = new ZendViewRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'ZendView'; + $template->addDefaultParam('zendview', 'name', $name); + $result = $template->render('zendview'); + + $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); + $content = str_replace('', $name, $content); + $this->assertEquals($content, $result); + } + + public function testAddSharedParameters() + { + $template = new ZendViewRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'ZendView'; + $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $result = $template->render('zendview'); + $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); + $content = str_replace('', $name, $content); + $this->assertEquals($content, $result); + + $result = $template->render('zendview-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/zendview-2.phtml'); + $content = str_replace('', $name, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersPerTemplate() + { + $template = new ZendViewRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Zend'; + $name2 = 'View'; + $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $template->addDefaultParam('zendview-2', 'name', $name2); + $result = $template->render('zendview'); + $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); + $content = str_replace('', $name, $content); + $this->assertEquals($content, $result); + + $result = $template->render('zendview-2'); + $content = file_get_contents(__DIR__ . '/TestAsset/zendview-2.phtml'); + $content = str_replace('', $name2, $content); + $this->assertEquals($content, $result); + } + + public function testOverrideSharedParametersAtRender() + { + $template = new ZendViewRenderer(); + $template->addPath(__DIR__ . '/TestAsset'); + $name = 'Zend'; + $name2 = 'View'; + $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $result = $template->render('zendview', ['name' => $name2]); + $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); + $content = str_replace('', $name2, $content); + $this->assertEquals($content, $result); + } } From 5e6561af4f8886d28ba03e8549213212094a5734 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sat, 10 Oct 2015 09:57:41 -0500 Subject: [PATCH 15/18] Make tests consistent - `s/$template/$renderer/` throughout new tests. --- test/Template/TwigRendererTest.php | 38 +++++++++++++------------- test/Template/ZendViewRendererTest.php | 38 +++++++++++++------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/test/Template/TwigRendererTest.php b/test/Template/TwigRendererTest.php index a9baee1a..4ecd0361 100644 --- a/test/Template/TwigRendererTest.php +++ b/test/Template/TwigRendererTest.php @@ -178,11 +178,11 @@ public function testResolvesNamespacedTemplateWithSuffix() public function testAddParameterToOneTemplate() { - $template = new TwigRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new TwigRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Twig'; - $template->addDefaultParam('twig', 'name', $name); - $result = $template->render('twig'); + $renderer->addDefaultParam('twig', 'name', $name); + $result = $renderer->render('twig'); $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); $content = str_replace('{{ name }}', $name, $content); @@ -191,16 +191,16 @@ public function testAddParameterToOneTemplate() public function testAddSharedParameters() { - $template = new TwigRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new TwigRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Twig'; - $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); - $result = $template->render('twig'); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); + $result = $renderer->render('twig'); $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); $content = str_replace('{{ name }}', $name, $content); $this->assertEquals($content, $result); - $result = $template->render('twig-2'); + $result = $renderer->render('twig-2'); $content = file_get_contents(__DIR__ . '/TestAsset/twig-2.html'); $content = str_replace('{{ name }}', $name, $content); $this->assertEquals($content, $result); @@ -208,18 +208,18 @@ public function testAddSharedParameters() public function testOverrideSharedParametersPerTemplate() { - $template = new TwigRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new TwigRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Twig'; $name2 = 'Template'; - $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); - $template->addDefaultParam('twig-2', 'name', $name2); - $result = $template->render('twig'); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); + $renderer->addDefaultParam('twig-2', 'name', $name2); + $result = $renderer->render('twig'); $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); $content = str_replace('{{ name }}', $name, $content); $this->assertEquals($content, $result); - $result = $template->render('twig-2'); + $result = $renderer->render('twig-2'); $content = file_get_contents(__DIR__ . '/TestAsset/twig-2.html'); $content = str_replace('{{ name }}', $name2, $content); $this->assertEquals($content, $result); @@ -227,12 +227,12 @@ public function testOverrideSharedParametersPerTemplate() public function testOverrideSharedParametersAtRender() { - $template = new TwigRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new TwigRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Twig'; $name2 = 'Template'; - $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); - $result = $template->render('twig', ['name' => $name2]); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); + $result = $renderer->render('twig', ['name' => $name2]); $content = file_get_contents(__DIR__ . '/TestAsset/twig.html'); $content = str_replace('{{ name }}', $name2, $content); $this->assertEquals($content, $result); diff --git a/test/Template/ZendViewRendererTest.php b/test/Template/ZendViewRendererTest.php index 7ce3439f..18c5bf3b 100644 --- a/test/Template/ZendViewRendererTest.php +++ b/test/Template/ZendViewRendererTest.php @@ -253,11 +253,11 @@ public function testProperlyResolvesNamespacedTemplate() public function testAddParameterToOneTemplate() { - $template = new ZendViewRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new ZendViewRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'ZendView'; - $template->addDefaultParam('zendview', 'name', $name); - $result = $template->render('zendview'); + $renderer->addDefaultParam('zendview', 'name', $name); + $result = $renderer->render('zendview'); $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); $content = str_replace('', $name, $content); @@ -266,16 +266,16 @@ public function testAddParameterToOneTemplate() public function testAddSharedParameters() { - $template = new ZendViewRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new ZendViewRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'ZendView'; - $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); - $result = $template->render('zendview'); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); + $result = $renderer->render('zendview'); $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); $content = str_replace('', $name, $content); $this->assertEquals($content, $result); - $result = $template->render('zendview-2'); + $result = $renderer->render('zendview-2'); $content = file_get_contents(__DIR__ . '/TestAsset/zendview-2.phtml'); $content = str_replace('', $name, $content); $this->assertEquals($content, $result); @@ -283,18 +283,18 @@ public function testAddSharedParameters() public function testOverrideSharedParametersPerTemplate() { - $template = new ZendViewRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new ZendViewRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Zend'; $name2 = 'View'; - $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); - $template->addDefaultParam('zendview-2', 'name', $name2); - $result = $template->render('zendview'); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); + $renderer->addDefaultParam('zendview-2', 'name', $name2); + $result = $renderer->render('zendview'); $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); $content = str_replace('', $name, $content); $this->assertEquals($content, $result); - $result = $template->render('zendview-2'); + $result = $renderer->render('zendview-2'); $content = file_get_contents(__DIR__ . '/TestAsset/zendview-2.phtml'); $content = str_replace('', $name2, $content); $this->assertEquals($content, $result); @@ -302,12 +302,12 @@ public function testOverrideSharedParametersPerTemplate() public function testOverrideSharedParametersAtRender() { - $template = new ZendViewRenderer(); - $template->addPath(__DIR__ . '/TestAsset'); + $renderer = new ZendViewRenderer(); + $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Zend'; $name2 = 'View'; - $template->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); - $result = $template->render('zendview', ['name' => $name2]); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); + $result = $renderer->render('zendview', ['name' => $name2]); $content = file_get_contents(__DIR__ . '/TestAsset/zendview.phtml'); $content = str_replace('', $name2, $content); $this->assertEquals($content, $result); From a6c19f6cbda4d4ec6e533cf8ac42341f03e8f385 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sat, 10 Oct 2015 11:28:54 -0500 Subject: [PATCH 16/18] Documented renderer default params --- doc/book/template/interface.md | 74 ++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/doc/book/template/interface.md b/doc/book/template/interface.md index b452acb6..48c9b886 100644 --- a/doc/book/template/interface.md +++ b/doc/book/template/interface.md @@ -41,6 +41,27 @@ interface TemplateRendererInterface * @return TemplatePath[] */ public function getPaths(); + + /** + * Add a default parameter to use with a template. + * + * Use this method to provide a default parameter to use when a template is + * rendered. The parameter may be overridden by providing it when calling + * `render()`, or by calling this method again with a null value. + * + * The parameter will be specific to the template name provided. To make + * the parameter available to any template, pass the TEMPLATE_ALL constant + * for the template name. + * + * If the default parameter existed previously, subsequent invocations with + * the same template name and parameter name will overwrite. + * + * @param string $templateName Name of template to which the param applies; + * use TEMPLATE_ALL to apply to all templates. + * @param string $param Param name. + * @param mixed $value + */ + public function addDefaultParam($templateName, $param, $value); } ``` @@ -119,3 +140,56 @@ $content = $renderer->render('message', [ It is up to the underlying template engine to determine how to perform the injections. + +### Default params + +The `TemplateRendererInterface` defines the method `addDefaultParam()`. This +method can be used to specify default parameters to use when rendering a +template. The signature is: + +```php +public function addDefaultParam($templateName, $param, $value) +``` + +If you want a parameter to be used for *every* template, you can specify the +constant `TemplateRendererInterface::TEMPLATE_ALL` for the `$templateName` +parameter. + +When rendering, parameters are considered in the following order, with later +items having precedence over earlier ones: + +- Default parameters specified for all templates. +- Default parameters specified for the template specified at rendering. +- Parameters specified when rendering. + +As an example, if we did the following: + +```php +$renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'foo', 'bar'); +$renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'bar', 'baz'); +$renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'baz', 'bat'); + +$renderer->addDefaultParam('example', 'foo', 'template default foo'); +$renderer->addDefaultParam('example', 'bar', 'template default bar'); + +$content = $renderer->render('example', [ + 'foo' => 'override', +]); +``` + +Then we can expect the following substitutions will occur when rendering: + +- References to the "foo" variable will contain "override". +- References to the "bar" variable will contain "template default bar". +- References to the "baz" variable will contain "bat". + +> #### Support for default params +> +> The support for default params will often be renderer-specific. The reason is +> because the `render()` signature does not specify a type for `$params`, in +> order to allow passing alternative arguments such as view models. In such +> cases, the implementation will indicate its behavior when default parameters +> are specified, but a given `$params` argument does not support it. +> +> At the time of writing, each of the Plates, Twig, and zend-view +> implementations support the feature. From 6d7f3b8d79ff831d568a378951df59240ed954ab Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sat, 10 Oct 2015 11:31:19 -0500 Subject: [PATCH 17/18] Added CHANGELOG for #143 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a05b4f8..16311a2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,12 @@ All notable changes to this project will be documented in this file, in reverse `Zend\Expressive\Template\Twig`. - `Zend\Expressive\Template\ZendViewRenderer`, replacing `Zend\Expressive\Template\ZendView`. +- [#143](https://github.com/zendframework/zend-expressive/pull/143) adds + the method `addDefaultParam($templateName, $param, $value)` to + `TemplateRendererInterface`, allowing users to specify global and + template-specific default parameters to use when rendering. To implement the + feature, the patch also provides `Zend\Expressive\Template\DefaultParamsTrait` + to simplify incorporating the feature in implementations. ### Deprecated From e9a7d2c3f102f7383716480eb35739875b7b7740 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sat, 10 Oct 2015 11:32:19 -0500 Subject: [PATCH 18/18] Fix failing test - due to invalid variable reference --- test/Template/PlatesRendererTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/Template/PlatesRendererTest.php b/test/Template/PlatesRendererTest.php index 6d52b33a..dc5aab51 100644 --- a/test/Template/PlatesRendererTest.php +++ b/test/Template/PlatesRendererTest.php @@ -213,7 +213,7 @@ public function testAddSharedParameters() $renderer = new PlatesRenderer(); $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; - $renderer->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name, $content); @@ -230,7 +230,7 @@ public function testOverrideSharedParametersPerTemplate() $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; $name2 = 'Saucers'; - $renderer->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); $renderer->addDefaultParam('plates-2', 'name', $name2); $result = $renderer->render('plates'); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); @@ -248,7 +248,7 @@ public function testOverrideSharedParametersAtRender() $renderer->addPath(__DIR__ . '/TestAsset'); $name = 'Plates'; $name2 = 'Saucers'; - $renderer->addDefaultParam($template::TEMPLATE_ALL, 'name', $name); + $renderer->addDefaultParam($renderer::TEMPLATE_ALL, 'name', $name); $result = $renderer->render('plates', ['name' => $name2]); $content = file_get_contents(__DIR__ . '/TestAsset/plates.php'); $content= str_replace('e($name)?>', $name2, $content);