22
33## PHP Renderer
44
5- This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 3 .
5+ This is a renderer for rendering PHP view scripts into a PSR-7 Response object. It works well with Slim Framework 4 .
66
77
88### Cross-site scripting (XSS) risks
@@ -17,28 +17,29 @@ Install with [Composer](http://getcomposer.org):
1717 composer require slim/php-view
1818
1919
20- ## Usage with Slim 3
20+ ## Usage with Slim 4
2121
2222``` php
2323use Slim\Views\PhpRenderer;
2424
2525include "vendor/autoload.php";
2626
27- $app = new Slim\App();
28- $container = $app->getContainer();
29- $container['renderer'] = new PhpRenderer("./templates");
27+ $app = Slim\AppFactor::creates();
3028
3129$app->get('/hello/{name}', function ($request, $response, $args) {
32- return $this->renderer->render($response, "hello.php", $args);
30+ $renderer = new PhpRenderer('path/to/templates');
31+ return $renderer->render($response, "hello.php", $args);
3332});
3433
3534$app->run();
3635```
3736
37+ Note that you could place the PhpRenderer instantiation within your DI Container.
38+
3839## Usage with any PSR-7 Project
3940``` php
4041//Construct the View
41- $phpView = new PhpRenderer("./ path/to/templates");
42+ $phpView = new PhpRenderer("path/to/templates");
4243
4344//Render a Template
4445$response = $phpView->render(new Response(), "hello.php", $yourData);
@@ -52,7 +53,7 @@ You can now add variables to your renderer that will be available to all templat
5253$templateVariables = [
5354 "title" => "Title"
5455];
55- $phpView = new PhpRenderer("./ path/to/templates", $templateVariables);
56+ $phpView = new PhpRenderer("path/to/templates", $templateVariables);
5657
5758// or setter
5859$phpView->setAttributes($templateVariables);
@@ -66,7 +67,7 @@ Data passed in via `->render()` takes precedence over attributes.
6667$templateVariables = [
6768 "title" => "Title"
6869];
69- $phpView = new PhpRenderer("./ path/to/templates", $templateVariables);
70+ $phpView = new PhpRenderer("path/to/templates", $templateVariables);
7071
7172//...
7273
@@ -83,19 +84,19 @@ Inside your templates you may use `$this` to refer to the PhpRenderer object to
8384You can now render view in another views called layouts, this allows you to compose modular view templates
8485and help keep your views DRY.
8586
86- Create your layout ` ./ path/to/templates/layout.php` .
87+ Create your layout ` path/to/templates/layout.php ` .
8788``` phtml
8889<html ><head ><title ><?=$title?></title ></head ><body ><?=$content?></body ></html >
8990```
9091
91- Create your view template ` ./ path/to/templates/hello.php` .
92+ Create your view template ` path/to/templates/hello.php ` .
9293``` phtml
9394Hello <?=$name?>!
9495```
9596
9697Rendering in your code.
9798``` php
98- $phpView = new PhpRenderer("./ path/to/templates", ["title" => "My App"]);
99+ $phpView = new PhpRenderer("path/to/templates", ["title" => "My App"]);
99100$phpView->setLayout("layout.php");
100101
101102//...
0 commit comments