Skip to content

Commit 1a992c4

Browse files
committed
Update readme
1 parent 27fa1ab commit 1a992c4

1 file changed

Lines changed: 84 additions & 35 deletions

File tree

README.md

Lines changed: 84 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ 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+
Install with Composer:
2122

2223
```
2324
composer require slim/php-view
@@ -28,13 +29,14 @@ composer require slim/php-view
2829
```php
2930
use Slim\Views\PhpRenderer;
3031

31-
include "vendor/autoload.php";
32+
include 'vendor/autoload.php';
3233

3334
$app = Slim\AppFactory::create();
3435

3536
$app->get('/hello/{name}', function ($request, $response, $args) {
3637
$renderer = new PhpRenderer('path/to/templates');
37-
return $renderer->render($response, "hello.php", $args);
38+
39+
return $renderer->render($response, 'hello.php', $args);
3840
});
3941

4042
$app->run();
@@ -46,87 +48,134 @@ Note that you could place the PhpRenderer instantiation within your DI Container
4648

4749
```php
4850
//Construct the View
49-
$phpView = new PhpRenderer("path/to/templates");
51+
$renderer = new PhpRenderer('path/to/templates');
52+
53+
$viewData = [
54+
'key1' => 'value1',
55+
'key2' => 'value2',
56+
];
5057

51-
//Render a Template
52-
$response = $phpView->render(new Response(), "hello.php", $yourData);
58+
// Render a template
59+
$response = $renderer->render(new Response(), 'hello.php', $viewData);
5360
```
5461

5562
## Template Variables
63+
5664
You can now add variables to your renderer that will be available to all templates you render.
5765

5866
```php
59-
// via the constructor
60-
$templateVariables = [
61-
"title" => "Title"
67+
// Via the constructor
68+
$globalViewData = [
69+
'title' => 'Title'
6270
];
63-
$phpView = new PhpRenderer("path/to/templates", $templateVariables);
71+
72+
$renderer = new PhpRenderer('path/to/templates', $globalViewData);
6473

6574
// or setter
66-
$phpView->setAttributes($templateVariables);
75+
$viewData = [
76+
'key1' => 'value1',
77+
'key2' => 'value2',
78+
];
79+
$renderer->setAttributes($viewData);
6780

6881
// or individually
69-
$phpView->addAttribute($key, $value);
82+
$renderer->addAttribute($key, $value);
7083
```
7184

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

7487
```php
75-
$templateVariables = [
76-
"title" => "Title"
88+
$viewData = [
89+
'title' => 'Title'
7790
];
78-
$phpView = new PhpRenderer("path/to/templates", $templateVariables);
91+
$renderer = new PhpRenderer('path/to/templates', $viewData);
7992

8093
//...
8194

82-
$phpView->render($response, $template, [
83-
"title" => "My Title"
95+
$response = $renderer->render($response, $template, [
96+
'title' => 'My Title'
8497
]);
98+
8599
// In the view above, the $title will be "My Title" and not "Title"
86100
```
87101

88102
## Sub-templates
89103

90104
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.
105+
If using a layout the `fetch()` method can be used instead of `render()` to avoid applying the layout to the sub-template.
92106

93-
```phtml
94-
<?=$this->fetch('./path/to/partial.phtml', ["name" => "John"])?>
107+
```php
108+
<?=$this->fetch('./path/to/partial.phtml', ['name' => 'John'])?>
95109
```
96110

97111
## Rendering in Layouts
98-
You can now render view in another views called layouts, this allows you to compose modular view templates
112+
113+
You can now render view in another views called layouts,
114+
this allows you to compose modular view templates
99115
and help keep your views DRY.
100116

101-
Create your layout `path/to/templates/layout.php`.
102-
```phtml
117+
Create your layout `path/to/templates/layout.php`
118+
119+
```php
103120
<html><head><title><?=$title?></title></head><body><?=$content?></body></html>
104121
```
105122

106-
Create your view template `path/to/templates/hello.php`.
107-
```phtml
123+
Create your view template `path/to/templates/hello.php`
124+
125+
```php
108126
Hello <?=$name?>!
109127
```
110128

111129
Rendering in your code.
130+
112131
```php
113-
$phpView = new PhpRenderer("path/to/templates", ["title" => "My App"]);
114-
$phpView->setLayout("layout.php");
132+
$renderer = new PhpRenderer('path/to/templates', ['title' => 'My App']);
133+
$renderer->setLayout('layout.php');
134+
135+
$viewData = [
136+
'title' => 'Hello - My App',
137+
'name' => 'John',
138+
];
115139

116140
//...
117141

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

121145
Response will be
146+
122147
```html
123148
<html><head><title>Hello - My App</title></head><body>Hello John!</body></html>
124149
```
125150

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.
151+
Please note, the `$content` is special variable used inside layouts
152+
to render the wrapped view and should not be set in your view parameters.
153+
154+
## Escaping values
155+
156+
It's essential to ensure that the HTML output is secure to
157+
prevent common web vulnerabilities like Cross-Site Scripting (XSS).
158+
This package has no built-in mitigation from XSS attacks.
159+
160+
The following function uses the `htmlspecialchars` function
161+
with specific flags to ensure proper encoding:
162+
163+
```php
164+
function html(string $text = null): string
165+
{
166+
return htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
167+
}
168+
```
169+
170+
You could consider setting it up as a global function in [composer.json](https://getcomposer.org/doc/04-schema.md#files).
171+
172+
**Usage**
173+
174+
```php
175+
Hello <?= html($name) ?>
176+
```
128177

129178
## Exceptions
130-
`\RuntimeException` - if template does not exist
131179

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

0 commit comments

Comments
 (0)