Skip to content

Commit 45a4b88

Browse files
authored
Merge pull request #106 from odan/readme
Update Readme
2 parents 27fa1ab + f3d9f7a commit 45a4b88

File tree

1 file changed

+112
-41
lines changed

1 file changed

+112
-41
lines changed

README.md

Lines changed: 112 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,121 +12,192 @@ This is a renderer for rendering PHP view scripts into a PSR-7 Response object.
1212

1313
### Cross-site scripting (XSS) risks
1414

15-
Note that PHP-View has no built-in mitigation from XSS attacks. It is the developer's responsibility to use `htmlspecialchars()` or a component like [laminas-escaper](https://github.com/laminas/laminas-escaper). Alternatively, consider [Twig-View](https://github.com/slimphp/Twig-View).
16-
15+
Note that PHP-View has no built-in mitigation from XSS attacks.
16+
It is the developer's responsibility to use `htmlspecialchars()`
17+
or a component like [laminas-escaper](https://github.com/laminas/laminas-escaper). Alternatively, consider [Twig-View](https://github.com/slimphp/Twig-View).
1718

1819
## Installation
1920

20-
Install with [Composer](http://getcomposer.org):
21-
2221
```
2322
composer require slim/php-view
2423
```
2524

25+
## Usage with any PSR-7 Project
26+
27+
```php
28+
//Construct the View
29+
$renderer = new PhpRenderer('path/to/templates');
30+
31+
$viewData = [
32+
'key1' => 'value1',
33+
'key2' => 'value2',
34+
];
35+
36+
// Render a template
37+
$response = $renderer->render(new Response(), 'hello.php', $viewData);
38+
```
39+
2640
## Usage with Slim 4
2741

2842
```php
43+
use Slim\AppFactory;
2944
use Slim\Views\PhpRenderer;
3045

31-
include "vendor/autoload.php";
46+
require __DIR__ . '/../vendor/autoload.php';
3247

33-
$app = Slim\AppFactory::create();
48+
$app = AppFactory::create();
3449

35-
$app->get('/hello/{name}', function ($request, $response, $args) {
50+
$app->get('/hello', function ($request, $response) {
3651
$renderer = new PhpRenderer('path/to/templates');
37-
return $renderer->render($response, "hello.php", $args);
52+
53+
$viewData = [
54+
'name' => 'John',
55+
];
56+
57+
return $renderer->render($response, 'hello.php', $viewData);
3858
});
3959

4060
$app->run();
4161
```
4262

43-
Note that you could place the PhpRenderer instantiation within your DI Container.
63+
## DI Container Setup
4464

45-
## Usage with any PSR-7 Project
65+
You can place the `PhpRenderer` instantiation within your DI Container.
4666

4767
```php
48-
//Construct the View
49-
$phpView = new PhpRenderer("path/to/templates");
68+
<?php
69+
70+
use Psr\Container\ContainerInterface;
71+
use Slim\Views\PhpRenderer;
72+
// ...
73+
74+
return [
75+
PhpRenderer::class => function (ContainerInterface $container) {
76+
$renderer = new PhpRenderer('path/to/templates');
77+
78+
return $renderer;
79+
},
80+
];
5081

51-
//Render a Template
52-
$response = $phpView->render(new Response(), "hello.php", $yourData);
5382
```
5483

5584
## Template Variables
85+
5686
You can now add variables to your renderer that will be available to all templates you render.
5787

5888
```php
59-
// via the constructor
60-
$templateVariables = [
61-
"title" => "Title"
89+
// Via the constructor
90+
$globalViewData = [
91+
'title' => 'Title'
6292
];
63-
$phpView = new PhpRenderer("path/to/templates", $templateVariables);
93+
94+
$renderer = new PhpRenderer('path/to/templates', $globalViewData);
6495

6596
// or setter
66-
$phpView->setAttributes($templateVariables);
97+
$viewData = [
98+
'key1' => 'value1',
99+
'key2' => 'value2',
100+
];
101+
$renderer->setAttributes($viewData);
67102

68103
// or individually
69-
$phpView->addAttribute($key, $value);
104+
$renderer->addAttribute($key, $value);
70105
```
71106

72-
Data passed in via `->render()` takes precedence over attributes.
107+
Data passed in via the `render()` method takes precedence over attributes.
73108

74109
```php
75-
$templateVariables = [
76-
"title" => "Title"
110+
$viewData = [
111+
'title' => 'Title'
77112
];
78-
$phpView = new PhpRenderer("path/to/templates", $templateVariables);
113+
$renderer = new PhpRenderer('path/to/templates', $viewData);
79114

80115
//...
81116

82-
$phpView->render($response, $template, [
83-
"title" => "My Title"
117+
$response = $renderer->render($response, $template, [
118+
'title' => 'My Title'
84119
]);
120+
85121
// In the view above, the $title will be "My Title" and not "Title"
86122
```
87123

88124
## Sub-templates
89125

90126
Inside your templates you may use `$this` to refer to the PhpRenderer object to render sub-templates.
91-
If using a layout the `fetch()` method can be used instead of `render()` to avoid appling the layout to the sub-template.
127+
If using a layout the `fetch()` method can be used instead of `render()` to avoid applying the layout to the sub-template.
92128

93-
```phtml
94-
<?=$this->fetch('./path/to/partial.phtml', ["name" => "John"])?>
129+
```php
130+
<?=$this->fetch('./path/to/partial.phtml', ['name' => 'John'])?>
95131
```
96132

97133
## Rendering in Layouts
98-
You can now render view in another views called layouts, this allows you to compose modular view templates
134+
135+
You can now render view in another views called layouts,
136+
this allows you to compose modular view templates
99137
and help keep your views DRY.
100138

101-
Create your layout `path/to/templates/layout.php`.
102-
```phtml
139+
Create your layout `path/to/templates/layout.php`
140+
141+
```php
103142
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
104143
```
105144

106-
Create your view template `path/to/templates/hello.php`.
107-
```phtml
145+
Create your view template `path/to/templates/hello.php`
146+
147+
```php
108148
Hello <?=$name?>!
109149
```
110150

111151
Rendering in your code.
152+
112153
```php
113-
$phpView = new PhpRenderer("path/to/templates", ["title" => "My App"]);
114-
$phpView->setLayout("layout.php");
154+
$renderer = new PhpRenderer('path/to/templates', ['title' => 'My App']);
155+
$renderer->setLayout('layout.php');
156+
157+
$viewData = [
158+
'title' => 'Hello - My App',
159+
'name' => 'John',
160+
];
115161

116162
//...
117163

118-
$phpview->render($response, "hello.php", ["title" => "Hello - My App", "name" => "John"]);
164+
$response = $renderer->render($response, 'hello.php', $viewData);
119165
```
120166

121167
Response will be
168+
122169
```html
123170
<html><head><title>Hello - My App</title></head><body>Hello John!</body></html>
124171
```
125172

126-
Please note, the $content is special variable used inside layouts to render the wrapped view and should not be set
127-
in your view paramaters.
173+
Please note, the `$content` is special variable used inside layouts
174+
to render the wrapped view and should not be set in your view parameters.
175+
176+
## Escaping values
177+
178+
It's essential to ensure that the HTML output is secure to
179+
prevent common web vulnerabilities like Cross-Site Scripting (XSS).
180+
This package has no built-in mitigation from XSS attacks.
181+
182+
The following function uses the `htmlspecialchars` function
183+
with specific flags to ensure proper encoding:
184+
185+
```php
186+
function html(string $text = null): string
187+
{
188+
return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
189+
}
190+
```
191+
192+
You could consider setting it up as a global function in [composer.json](https://getcomposer.org/doc/04-schema.md#files).
193+
194+
**Usage**
195+
196+
```php
197+
Hello <?= html($name) ?>
198+
```
128199

129200
## Exceptions
130-
`\RuntimeException` - if template does not exist
131201

132-
`\InvalidArgumentException` - if $data contains 'template'
202+
* `\RuntimeException` - If template does not exist
203+
* `\InvalidArgumentException` - If $data contains 'template'

0 commit comments

Comments
 (0)