From 12a771c716d20a019506cefc2bc2c8ce2c7b411f Mon Sep 17 00:00:00 2001 From: Glenn Eggleton Date: Thu, 3 Mar 2016 10:47:19 -0500 Subject: [PATCH 1/3] Added Attributes to Renderer, updated PHPUnit version --- composer.json | 2 +- src/PhpRenderer.php | 56 ++++++++++++++++++++++++++++++++++++++- tests/PhpRendererTest.php | 50 +++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 32b5f7d..edb2b1f 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^4.0", + "phpunit/phpunit": "^5.0", "slim/slim" : "^3.0" } } diff --git a/src/PhpRenderer.php b/src/PhpRenderer.php index 4a14a47..94c5f3c 100644 --- a/src/PhpRenderer.php +++ b/src/PhpRenderer.php @@ -8,6 +8,7 @@ */ namespace Slim\Views; +use \InvalidArgumentException; use Psr\Http\Message\ResponseInterface; /** @@ -23,14 +24,21 @@ class PhpRenderer */ protected $templatePath; + /** + * @var array + */ + protected $attributes; + /** * SlimRenderer constructor. * * @param string $templatePath + * @param array $attributes */ - public function __construct($templatePath = "") + public function __construct($templatePath = "", $attributes = []) { $this->templatePath = $templatePath; + $this->attributes = $attributes; } /** @@ -58,6 +66,46 @@ public function render(ResponseInterface $response, $template, array $data = []) return $response; } + /** + * Get the attributes for the renderer + * + * @return array + */ + public function getAttributes() + { + return $this->attributes; + } + + /** + * Set the attributes for the renderer + * + * @param array $attributes + */ + public function setAttributes(array $attributes) + { + $this->attributes = $attributes; + } + + /** + * Get the template path + * + * @return string + */ + public function getTemplatePath() + { + return $this->templatePath; + } + + /** + * Set the template path + * + * @param string $templatePath + */ + public function setTemplatePath($templatePath) + { + $this->templatePath = $templatePath; + } + /** * Renders a template and returns the result as a string * @@ -82,6 +130,12 @@ public function fetch($template, array $data = []) { throw new \RuntimeException("View cannot render `$template` because the template does not exist"); } + foreach ($data as $k=>$val) { + if (in_array($k, array_keys($this->attributes))) { + throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k); + } + } + ob_start(); $this->protectedIncludeScope($this->templatePath . $template, $data); $output = ob_get_clean(); diff --git a/tests/PhpRendererTest.php b/tests/PhpRendererTest.php index cc1226c..56a8a15 100644 --- a/tests/PhpRendererTest.php +++ b/tests/PhpRendererTest.php @@ -9,7 +9,7 @@ * Date: 2015-11-12 * Time: 1:19 PM */ -class PhpRendererTest extends \PHPUnit_Framework_TestCase +class PhpRendererTest extends PHPUnit_Framework_TestCase { public function testRenderer() { @@ -25,4 +25,52 @@ public function testRenderer() { $this->assertEquals("Hi", $newResponse->getBody()->getContents()); } + + /** + * @expectedException InvalidArgumentException + */ + public function testExceptionForAttr() { + + $renderer = new \Slim\Views\PhpRenderer("tests/", [ + "hello" => "Hi" + ]); + + $headers = new Headers(); + $body = new Body(fopen('php://temp', 'r+')); + $response = new Response(200, $headers, $body); + + $renderer->render($response, "testTemplate.php", [ + "hello" => "Hi" + ]); + } + + /** + * @expectedException InvalidArgumentException + */ + public function testExceptionForTemplateInData() { + + $renderer = new \Slim\Views\PhpRenderer("tests/"); + + $headers = new Headers(); + $body = new Body(fopen('php://temp', 'r+')); + $response = new Response(200, $headers, $body); + + $renderer->render($response, "testTemplate.php", [ + "template" => "Hi" + ]); + } + + /** + * @expectedException RuntimeException + */ + public function testTemplateNotFound() { + + $renderer = new \Slim\Views\PhpRenderer("tests/"); + + $headers = new Headers(); + $body = new Body(fopen('php://temp', 'r+')); + $response = new Response(200, $headers, $body); + + $renderer->render($response, "adfadftestTemplate.php", []); + } } From 6b31dbd9c41421de527f0223dc63b0895c74c8bc Mon Sep 17 00:00:00 2001 From: Glenn Eggleton Date: Thu, 3 Mar 2016 11:29:16 -0500 Subject: [PATCH 2/3] Changed Attributes to merge in with data, updated ReadMe --- README.md | 33 +++++++++++++++++++++++++++++++++ src/PhpRenderer.php | 28 ++++++++++++++++++++++++++++ tests/PhpRendererTest.php | 11 +++++------ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 040fa53..cf4b52b 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,39 @@ $phpView = new PhpRenderer("./path/to/templates"); $response = $phpView->render(new Response(), "/path/to/template.php", $yourData); ``` +## Template Variables + +You can now add variables to your renderer that will be available to all templates you render. + +```php +//Via the constructor +$templateVariables = [ + "title" => "Title" +]; +$phpView = new PhpRenderer("./path/to/templates", $templateVariables); + +//Or Setter +$phpView->setAttributes($templateVariables); + +//Or Individually +$phpView->addAttribute($key, $value); +``` + +Data passed in via `->render()` takes precedence over attributes. +```php +$templateVariables = [ + "title" => "Title" +]; +$phpView = new PhpRenderer("./path/to/templates", $templateVariables); + +//... + +$phpView->render($response, $template, [ + "title" => "My Title" +]); +// In the view above, the $title will be "My Title" and not "Title" +``` + ## Exceptions `\RuntimeException` - if template does not exist diff --git a/src/PhpRenderer.php b/src/PhpRenderer.php index 94c5f3c..eedaa4b 100644 --- a/src/PhpRenderer.php +++ b/src/PhpRenderer.php @@ -86,6 +86,30 @@ public function setAttributes(array $attributes) $this->attributes = $attributes; } + /** + * Add an attribute + * + * @param $key + * @param $value + */ + public function addAttribute($key, $value) { + $this->attributes[$key] = $value; + } + + /** + * Retrieve an attribute + * + * @param $key + * @return mixed + */ + public function getAttribute($key) { + if (!isset($this->attributes[$key])) { + return false; + } + + return $this->attributes[$key]; + } + /** * Get the template path * @@ -130,11 +154,15 @@ public function fetch($template, array $data = []) { throw new \RuntimeException("View cannot render `$template` because the template does not exist"); } + + /* foreach ($data as $k=>$val) { if (in_array($k, array_keys($this->attributes))) { throw new \InvalidArgumentException("Duplicate key found in data and renderer attributes. " . $k); } } + */ + $data = array_merge($this->attributes, $data); ob_start(); $this->protectedIncludeScope($this->templatePath . $template, $data); diff --git a/tests/PhpRendererTest.php b/tests/PhpRendererTest.php index 56a8a15..6ec92a6 100644 --- a/tests/PhpRendererTest.php +++ b/tests/PhpRendererTest.php @@ -26,22 +26,21 @@ public function testRenderer() { $this->assertEquals("Hi", $newResponse->getBody()->getContents()); } - /** - * @expectedException InvalidArgumentException - */ - public function testExceptionForAttr() { + public function testAttributeMerging() { $renderer = new \Slim\Views\PhpRenderer("tests/", [ - "hello" => "Hi" + "hello" => "Hello" ]); $headers = new Headers(); $body = new Body(fopen('php://temp', 'r+')); $response = new Response(200, $headers, $body); - $renderer->render($response, "testTemplate.php", [ + $newResponse = $renderer->render($response, "testTemplate.php", [ "hello" => "Hi" ]); + $newResponse->getBody()->rewind(); + $this->assertEquals("Hi", $newResponse->getBody()->getContents()); } /** From ecddeebc5a1eed7a8b6cad35cd1d8a938d364391 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Fri, 4 Mar 2016 09:48:44 +0000 Subject: [PATCH 3/3] Minor tidup up to README --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cf4b52b..cbe6896 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,16 @@ $response = $phpView->render(new Response(), "/path/to/template.php", $yourData) You can now add variables to your renderer that will be available to all templates you render. ```php -//Via the constructor +// via the constructor $templateVariables = [ "title" => "Title" ]; $phpView = new PhpRenderer("./path/to/templates", $templateVariables); -//Or Setter +// or setter $phpView->setAttributes($templateVariables); -//Or Individually +// or individually $phpView->addAttribute($key, $value); ```