Skip to content

Commit

Permalink
Merge branch 'geggleto-master'
Browse files Browse the repository at this point in the history
Closes #19
  • Loading branch information
akrabat committed Mar 3, 2016
2 parents f5134be + 8648440 commit 444aeeb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 3.


## Templates
You may use `$this` inside your php templates. `$this` will be the actual PhpRenderer object will allow you to render sub-templates

## Installation

Install with [Composer](http://getcomposer.org):

composer require slim/php-view


## Usage With Slim 3

```php
Expand Down
52 changes: 39 additions & 13 deletions src/PhpRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
use Psr\Http\Message\ResponseInterface;

/**
* Php View
* Class PhpRenderer
* @package Slim\Views
*
* Render PHP view scripts into a PSR-7 Response object
*/
Expand Down Expand Up @@ -39,8 +40,8 @@ public function __construct($templatePath = "")
*
* throws RuntimeException if $templatePath . $template does not exist
*
* @param \ResponseInterface $response
* @param $template
* @param ResponseInterface $response
* @param string $template
* @param array $data
*
* @return ResponseInterface
Expand All @@ -50,6 +51,29 @@ public function __construct($templatePath = "")
*/
public function render(ResponseInterface $response, $template, array $data = [])
{
$output = $this->fetch($template, $data);

$response->getBody()->write($output);

return $response;
}

/**
* Renders a template and returns the result as a string
*
* cannot contain template as a key
*
* throws RuntimeException if $templatePath . $template does not exist
*
* @param $template
* @param array $data
*
* @return mixed
*
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function fetch($template, array $data = []) {
if (isset($data['template'])) {
throw new \InvalidArgumentException("Duplicate template key found");
}
Expand All @@ -58,17 +82,19 @@ public function render(ResponseInterface $response, $template, array $data = [])
throw new \RuntimeException("View cannot render `$template` because the template does not exist");
}

$render = function ($template, $data) {
extract($data);
include $template;
};

ob_start();
$render($this->templatePath . $template, $data);
$output = ob_get_clean();
$this->protectedIncludeScope($this->templatePath . $template, $data);
$output = ob_get_clean();

$response->getBody()->write($output);

return $response;
return $output;
}

/**
* @param string $template
* @param array $data
*/
protected function protectedIncludeScope ($template, array $data) {
extract($data);
include $template;
}
}
2 changes: 1 addition & 1 deletion tests/testTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
* Date: 2015-11-12
* Time: 1:20 PM
*/
print $hello;
print $hello;

0 comments on commit 444aeeb

Please sign in to comment.