Skip to content

Commit dbf7327

Browse files
committed
Merge branch 'master' of https://github.com/slimphp/PHP-View into textual-change
# Conflicts: # README.md
2 parents 1d81787 + 8401136 commit dbf7327

6 files changed

Lines changed: 187 additions & 16 deletions

File tree

.travis.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
language: php
22

3-
php:
4-
- 5.5
5-
- 5.6
6-
- 7.0
7-
- hhvm
3+
dist: trusty
4+
5+
matrix:
6+
include:
7+
- php: 7.1
8+
env: ANALYSIS='true'
9+
- php: 7.2
10+
- php: 7.3
11+
- php: nightly
12+
allow_failures:
13+
- php: nightly
814

915
before_script: composer install
1016

11-
script: phpunit --coverage-text --configuration phpunit.xml.dist
17+
script: vendor/bin/phpunit --coverage-text --configuration phpunit.xml.dist

README.md

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $container = $app->getContainer();
2929
$container['renderer'] = new PhpRenderer("./templates");
3030

3131
$app->get('/hello/{name}', function ($request, $response, $args) {
32-
return $this->renderer->render($response, "/hello.php", $args);
32+
return $this->renderer->render($response, "hello.php", $args);
3333
});
3434

3535
$app->run();
@@ -41,11 +41,10 @@ $app->run();
4141
$phpView = new PhpRenderer("./path/to/templates");
4242

4343
//Render a Template
44-
$response = $phpView->render(new Response(), "/path/to/template.php", $yourData);
44+
$response = $phpView->render(new Response(), "hello.php", $yourData);
4545
```
4646

4747
## Template Variables
48-
4948
You can now add variables to your renderer that will be available to all templates you render.
5049

5150
```php
@@ -80,6 +79,37 @@ $phpView->render($response, $template, [
8079
## Sub-templates
8180
Inside your templates you may use `$this` to refer to the PhpRenderer object to render sub-templates.
8281

82+
## Rendering in Layouts
83+
You can now render view in another views called layouts, this allows you to compose modular view templates
84+
and help keep your views DRY.
85+
86+
Create your layout `./path/to/templates/layout.php`.
87+
```phtml
88+
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
89+
```
90+
91+
Create your view template `./path/to/templates/hello.php`.
92+
```phtml
93+
Hello <?=$name?>!
94+
```
95+
96+
Rendering in your code.
97+
```php
98+
$phpView = new PhpRenderer("./path/to/templates", ["title" => "My App"]);
99+
$phpView->setLayout("layout.php");
100+
101+
//...
102+
103+
$phpview->render($response, "hello.php", ["title" => "Hello - My App", "name" => "John"]);
104+
```
105+
106+
Response will be
107+
```html
108+
<html><head><title>Hello - My App</title></head><body>Hello John!</body></html>
109+
```
110+
111+
Please note, the $content is special variable used inside layouts to render the wrapped view and should not be set
112+
in your view paramaters.
83113

84114
## Exceptions
85115
`\RuntimeException` - if template does not exist

phpunit.xml.dist

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22

3-
<phpunit backupGlobals="false"
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.7/phpunit.xsd"
5+
backupGlobals="false"
46
backupStaticAttributes="false"
7+
beStrictAboutTestsThatDoNotTestAnything="true"
8+
beStrictAboutChangesToGlobalState="true"
9+
beStrictAboutOutputDuringTests="true"
510
colors="true"
611
convertErrorsToExceptions="true"
712
convertNoticesToExceptions="true"
813
convertWarningsToExceptions="true"
914
processIsolation="false"
1015
stopOnFailure="false"
11-
syntaxCheck="false"
1216
bootstrap="tests/bootstrap.php"
1317
>
1418
<testsuites>
15-
<testsuite name="Renderer Tests">
16-
<directory>tests/</directory>
19+
<testsuite name="PHP-View Test Suite">
20+
<directory>./tests/</directory>
1721
</testsuite>
1822
</testsuites>
1923

2024
<filter>
21-
<whitelist>
22-
<directory>src/</directory>
25+
<whitelist processUncoveredFilesFromWhitelist="true">
26+
<directory>./src/</directory>
2327
</whitelist>
2428
</filter>
2529
</phpunit>

src/PhpRenderer.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,23 @@ class PhpRenderer
2929
*/
3030
protected $attributes;
3131

32+
/**
33+
* @var string
34+
*/
35+
protected $layout;
36+
3237
/**
3338
* SlimRenderer constructor.
3439
*
3540
* @param string $templatePath
3641
* @param array $attributes
42+
* @param string $layout
3743
*/
38-
public function __construct($templatePath = "", $attributes = [])
44+
public function __construct($templatePath = "", $attributes = [], $layout = "")
3945
{
4046
$this->templatePath = rtrim($templatePath, '/\\') . '/';
4147
$this->attributes = $attributes;
48+
$this->setLayout($layout);
4249
}
4350

4451
/**
@@ -66,6 +73,34 @@ public function render(ResponseInterface $response, $template, array $data = [])
6673
return $response;
6774
}
6875

76+
/**
77+
* Get layout template
78+
*
79+
* @return string
80+
*/
81+
public function getLayout()
82+
{
83+
return $this->layout;
84+
}
85+
86+
/**
87+
* Set layout template
88+
*
89+
* @param string $layout
90+
*/
91+
public function setLayout($layout)
92+
{
93+
if ($layout === "" || $layout === null) {
94+
$this->layout = null;
95+
} else {
96+
$layoutPath = $this->templatePath . $layout;
97+
if (!is_file($layoutPath)) {
98+
throw new \RuntimeException("Layout template `$layout` does not exist");
99+
}
100+
$this->layout = $layoutPath;
101+
}
102+
}
103+
69104
/**
70105
* Get the attributes for the renderer
71106
*
@@ -168,6 +203,13 @@ public function fetch($template, array $data = []) {
168203
ob_start();
169204
$this->protectedIncludeScope($this->templatePath . $template, $data);
170205
$output = ob_get_clean();
206+
207+
if ($this->layout !== null) {
208+
ob_start();
209+
$data['content'] = $output;
210+
$this->protectedIncludeScope($this->layout, $data);
211+
$output = ob_get_clean();
212+
}
171213
} catch(\Throwable $e) { // PHP 7+
172214
ob_end_clean();
173215
throw $e;

tests/PhpRendererTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,92 @@ public function testTemplateNotFound() {
112112

113113
$renderer->render($response, "adfadftestTemplate.php", []);
114114
}
115+
116+
public function testLayout() {
117+
$renderer = new \Slim\Views\PhpRenderer("tests/", ["title" => "My App"]);
118+
$renderer->setLayout("testLayout.php");
119+
120+
$headers = new Headers();
121+
$body = new Body(fopen('php://temp', 'r+'));
122+
$response = new Response(200, $headers, $body);
123+
124+
$newResponse = $renderer->render($response, "testTemplate.php", array("title" => "Hello - My App", "hello" => "Hi"));
125+
126+
$newResponse->getBody()->rewind();
127+
128+
$this->assertEquals("<html><head><title>Hello - My App</title></head><body>Hi</body></html>", $newResponse->getBody()->getContents());
129+
}
130+
131+
public function testLayoutConstructor() {
132+
$renderer = new \Slim\Views\PhpRenderer("tests", ["title" => "My App"], "testLayout.php");
133+
134+
$headers = new Headers();
135+
$body = new Body(fopen('php://temp', 'r+'));
136+
$response = new Response(200, $headers, $body);
137+
138+
$newResponse = $renderer->render($response, "testTemplate.php", array("title" => "Hello - My App", "hello" => "Hi"));
139+
140+
$newResponse->getBody()->rewind();
141+
142+
$this->assertEquals("<html><head><title>Hello - My App</title></head><body>Hi</body></html>", $newResponse->getBody()->getContents());
143+
}
144+
145+
public function testExceptionInLayout() {
146+
$renderer = new \Slim\Views\PhpRenderer("tests/");
147+
$renderer->setLayout("testException.php");
148+
149+
$headers = new Headers();
150+
$body = new Body(fopen('php://temp', 'r+'));
151+
$response = new Response(200, $headers, $body);
152+
153+
try {
154+
$newResponse = $renderer->render($response, "testTemplate.php");
155+
} catch (Throwable $t) { // PHP 7+
156+
// Simulates an error template
157+
$renderer->setLayout(null);
158+
$newResponse = $renderer->render($response, "testTemplate.php", [
159+
"hello" => "Hi"
160+
]);
161+
} catch (Exception $e) { // PHP < 7
162+
// Simulates an error template
163+
$renderer->setLayout(null);
164+
$newResponse = $renderer->render($response, "testTemplate.php", [
165+
"hello" => "Hi"
166+
]);
167+
}
168+
169+
$newResponse->getBody()->rewind();
170+
171+
$this->assertEquals("Hi", $newResponse->getBody()->getContents());
172+
}
173+
174+
/**
175+
* @expectedException RuntimeException
176+
*/
177+
public function testLayoutNotFound() {
178+
179+
$renderer = new \Slim\Views\PhpRenderer("tests/");
180+
$renderer->setLayout("adfadftestLayout.php");
181+
182+
$headers = new Headers();
183+
$body = new Body(fopen('php://temp', 'r+'));
184+
$response = new Response(200, $headers, $body);
185+
186+
$renderer->render($response, "testTemplate.php", []);
187+
}
188+
189+
public function testContentDataKeyShouldBeIgnored() {
190+
$renderer = new \Slim\Views\PhpRenderer("tests/");
191+
$renderer->setLayout("testLayout.php");
192+
193+
$headers = new Headers();
194+
$body = new Body(fopen('php://temp', 'r+'));
195+
$response = new Response(200, $headers, $body);
196+
197+
$newResponse = $renderer->render($response, "testTemplate.php", array("title" => "Hello - My App", "hello" => "Hi", "content" => "Ho"));
198+
199+
$newResponse->getBody()->rewind();
200+
201+
$this->assertEquals("<html><head><title>Hello - My App</title></head><body>Hi</body></html>", $newResponse->getBody()->getContents());
202+
}
115203
}

tests/testLayout.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>

0 commit comments

Comments
 (0)