Skip to content

Commit

Permalink
Merge branch 'geggleto-master'
Browse files Browse the repository at this point in the history
Closes #22
  • Loading branch information
akrabat committed Mar 4, 2016
2 parents 444aeeb + ecddeeb commit 8bae5b1
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.0",
"phpunit/phpunit": "^5.0",
"slim/slim" : "^3.0"
}
}
84 changes: 83 additions & 1 deletion src/PhpRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Slim\Views;

use \InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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();
Expand Down
49 changes: 48 additions & 1 deletion tests/PhpRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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", []);
}
}

0 comments on commit 8bae5b1

Please sign in to comment.