diff --git a/README.md b/README.md index 040fa53..cbe6896 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/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..eedaa4b 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,70 @@ 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; + } + + /** + * 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 + * + * @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 +154,16 @@ 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); $output = ob_get_clean(); diff --git a/tests/PhpRendererTest.php b/tests/PhpRendererTest.php index cc1226c..6ec92a6 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,51 @@ public function testRenderer() { $this->assertEquals("Hi", $newResponse->getBody()->getContents()); } + + public function testAttributeMerging() { + + $renderer = new \Slim\Views\PhpRenderer("tests/", [ + "hello" => "Hello" + ]); + + $headers = new Headers(); + $body = new Body(fopen('php://temp', 'r+')); + $response = new Response(200, $headers, $body); + + $newResponse = $renderer->render($response, "testTemplate.php", [ + "hello" => "Hi" + ]); + $newResponse->getBody()->rewind(); + $this->assertEquals("Hi", $newResponse->getBody()->getContents()); + } + + /** + * @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", []); + } }