Skip to content

Commit 6b6d22a

Browse files
committed
Update README.md and now 'render()' method will thrown 'LogicException' if response instance not been set
closes #5 Signed-off-by: Fery Wardiyanto <[email protected]>
1 parent 82c3005 commit 6b6d22a

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ $container->register(new \Projek\Slim\PlatesProvider);
3636

3737
// Option 2, using Closure
3838
$container['view'] = function ($c) {
39-
$settings = [
39+
$view = new \Projek\Slim\Plates([
4040
// Path to view directory (default: null)
4141
'directory' => 'path/to/views',
4242
// Path to asset directory (default: null)
@@ -45,24 +45,24 @@ $container['view'] = function ($c) {
4545
'fileExtension' => 'tpl',
4646
// Template extension (default: false) see: http://platesphp.com/extensions/asset/
4747
'timestampInFilename' => false,
48-
];
49-
$view = new \Projek\Slim\Plates($settings);
48+
]);
5049

5150
// Set \Psr\Http\Message\ResponseInterface object
52-
// Or you can optionaly pass `$c['response']` in `__construct` second parameter
53-
$view->setResponse($c['response']);
51+
// Or you can optionaly pass `$c->get('response')` in `__construct` second parameter
52+
$view->setResponse($c->get('response'));
5453

5554
// Instantiate and add Slim specific extension
56-
$view->loadExtension(
57-
new Projek\Slim\PlatesExtension($c['router'], $c['request']->getUri())
58-
);
55+
$view->loadExtension(new Projek\Slim\PlatesExtension(
56+
$c->get('router'),
57+
$c->get('request')->getUri()
58+
));
5959

6060
return $view;
6161
};
6262

6363
// Define named route
6464
$app->get('/hello/{name}', function ($request, $response, $args) {
65-
return $this->view->render($response, 'profile', [
65+
return $this->view->render('profile', [
6666
'name' => $args['name']
6767
]);
6868
})->setName('profile');
@@ -71,7 +71,9 @@ $app->get('/hello/{name}', function ($request, $response, $args) {
7171
$app->run();
7272
```
7373

74-
**NOTE**: if you are using _option 1_ please make sure you already have `$container['settings']['view']` in your configuration file.
74+
**NOTE**:
75+
* If you are using _option 1_ please make sure you already have `$container['settings']['view']` in your configuration file.
76+
* `Plates::setResponse()` is required to use `Plates::render()` otherwise `\LogicException` will thrown.
7577

7678
## Custom functions
7779

src/Plates.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function loadExtension(ExtensionInterface $extension)
8888
* @param string $directory
8989
* @param boolean $fallback
9090
* @return \League\Plates\Engine
91+
* @throws \LogicException
9192
*/
9293
public function addFolder($name, $directory, $fallback = false)
9394
{
@@ -100,6 +101,7 @@ public function addFolder($name, $directory, $fallback = false)
100101
* @param array $data
101102
* @param null|string[] $templates
102103
* @return \League\Plates\Engine
104+
* @throws \LogicException
103105
*/
104106
public function addData(array $data, $templates = null)
105107
{
@@ -112,6 +114,7 @@ public function addData(array $data, $templates = null)
112114
* @param string $name
113115
* @param callable $callback
114116
* @return \League\Plates\Engine
117+
* @throws \LogicException
115118
*/
116119
public function registerFunction($name, $callback)
117120
{
@@ -137,9 +140,16 @@ public function setResponse(ResponseInterface $response)
137140
* @param string $name
138141
* @param string[] $data
139142
* @return \Psr\Http\Message\ResponseInterface
143+
* @throws \LogicException
140144
*/
141145
public function render($name, array $data = [])
142146
{
147+
if (! isset($this->response)) {
148+
throw new \LogicException(
149+
sprintf('Invalid %s object instance', ResponseInterface::class)
150+
);
151+
}
152+
143153
return $this->response->write($this->plates->render($name, $data));
144154
}
145155
}

tests/PlatesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@ public function testRender()
4040

4141
$this->assertInstanceOf(ResponseInterface::class, $response);
4242
}
43+
44+
/**
45+
* @expectedException \LogicException
46+
*/
47+
public function testInvalidResponseObj()
48+
{
49+
$response = $this->view->render('example', [
50+
'name' => 'Fery'
51+
]);
52+
}
4353
}

0 commit comments

Comments
 (0)