Skip to content

Commit 6703eba

Browse files
authored
Fix #226: Fix reset config for referenced definitions, adjust config to make View and WebView more configurable
1 parent d211ca6 commit 6703eba

10 files changed

Lines changed: 94 additions & 74 deletions

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## 8.0.1 under development
44

55
- Bug #224: Fix signature of `CachedContent::cache()` (@vjik)
6+
- Bug #226: Fix `reset` config for referenced definitions (@rustamwin)
7+
- Enh #226: Adjust config to make `View` and `WebView` more configurable (@rustamwin)
68

79
## 8.0.0 February 16, 2023
810

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
[![type-coverage](https://shepherd.dev/github/yiisoft/view/coverage.svg)](https://shepherd.dev/github/yiisoft/view)
1717

1818
This library provides templates rendering abstraction supporting layout-view-subview hierarchy, custom renderers with
19-
PHP-based as default and more. It is used in [Yii Framework](https://www.yiiframework.com/) but is supposed
20-
to be usable separately.
19+
PHP-based as default, and more. It's used in [Yii Framework](https://www.yiiframework.com/) but is usable separately.
2120

2221
## Requirements
2322

@@ -36,7 +35,7 @@ composer require yiisoft/view
3635
The package provides two use cases for managing view templates:
3736

3837
- [Basic functionality](docs/basic-functionality.md) for use in any environment.
39-
- Advanced functionality for [use in a WEB environment](docs/use-in-web-environment.md).
38+
- Advanced functionality for [use in a web environment](docs/use-in-web-environment.md).
4039

4140
### State of `View` and `WebView` services
4241

@@ -59,7 +58,7 @@ data.
5958
- JS/CSS strings,
6059
- JS/CSS files.
6160

62-
The state of `View` and `WebView` is not cloned when the services are cloned. So when
61+
The state of `View` and `WebView` isn't cloned when the services are cloned. So when
6362
using `with*()`, both new and old instances are sharing the same set of stateful mutable data. It allows, for example,
6463
to get `WebView` via type-hinting in a controller and change context path:
6564

@@ -72,7 +71,7 @@ final class BlogController {
7271
}
7372
```
7473

75-
... and then register CSS in a widget:
74+
and then register CSS in a widget:
7675

7776
```php
7877
final class LastPosts extends Widget
@@ -144,9 +143,10 @@ $view = $view->withClearedState();
144143

145144
## Extensions
146145

147-
- [yiisoft/yii-view](https://github.com/yiisoft/yii-view) - a wrapper that is used in
148-
[Yii Framework]((https://www.yiiframework.com/)). Adds additional functionality for a WEB
149-
environment and compatibility with [PSR-7](https://www.php-fig.org/psr/psr-7) interfaces.
146+
- [yiisoft/yii-view](https://github.com/yiisoft/yii-view) - a wrapper that's used in
147+
[Yii Framework]((https://www.yiiframework.com/)).
148+
Adds extra functionality for a web environment and compatibility
149+
with [PSR-7](https://www.php-fig.org/psr/psr-7) interfaces.
150150
- [yiisoft/view-twig](https://github.com/yiisoft/view-twig) - an extension that provides a view renderer that will
151151
allow you to use the [Twig](https://twig.symfony.com) view template engine, instead of the default PHP renderer.
152152

@@ -179,7 +179,7 @@ The code is statically analyzed with [Psalm](https://psalm.dev/). To run static
179179

180180
## License
181181

182-
The Yii View Rendering Library is free software. It is released under the terms of the BSD License.
182+
The Yii View Rendering Library is free software. It's released under the terms of the BSD License.
183183
Please see [`LICENSE`](./LICENSE.md) for more information.
184184

185185
Maintained by [Yii Software](https://www.yiiframework.com/).

config/di-web.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
declare(strict_types=1);
44

5+
use Psr\Container\ContainerInterface;
56
use Yiisoft\Aliases\Aliases;
7+
use Yiisoft\Definitions\Contract\ReferenceInterface;
68
use Yiisoft\Definitions\DynamicReference;
79
use Yiisoft\View\Theme;
810
use Yiisoft\View\WebView;
@@ -30,13 +32,19 @@
3032
static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath'])
3133
),
3234
],
33-
'setParameters()' => [
34-
$params['yiisoft/view']['parameters'],
35-
],
36-
'reset' => function () use ($params) {
35+
'setParameters()' => [$params['yiisoft/view']['parameters']],
36+
'withRenderers()' => [$params['yiisoft/view']['renderers']],
37+
'withDefaultExtension()' => [$params['yiisoft/view']['defaultExtension']],
38+
'reset' => function (ContainerInterface $container) use ($params) {
3739
/** @var WebView $this */
3840
$this->clear();
39-
$this->setParameters($params['yiisoft/view']['parameters']);
41+
$parameters = $params['yiisoft/view']['parameters'];
42+
foreach ($parameters as $name => $parameter) {
43+
$parameters[$name] = $parameter instanceof ReferenceInterface ?
44+
$parameter->resolve($container) :
45+
$parameter;
46+
}
47+
$this->setParameters($parameters);
4048
},
4149
],
4250
];

config/di.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
declare(strict_types=1);
44

5+
use Psr\Container\ContainerInterface;
56
use Yiisoft\Aliases\Aliases;
7+
use Yiisoft\Definitions\Contract\ReferenceInterface;
68
use Yiisoft\Definitions\DynamicReference;
79
use Yiisoft\View\View;
810

@@ -15,13 +17,19 @@
1517
static fn (Aliases $aliases) => $aliases->get($params['yiisoft/view']['basePath'])
1618
),
1719
],
18-
'setParameters()' => [
19-
$params['yiisoft/view']['parameters'],
20-
],
21-
'reset' => function () use ($params) {
20+
'setParameters()' => [$params['yiisoft/view']['parameters']],
21+
'withRenderers()' => [$params['yiisoft/view']['renderers']],
22+
'withDefaultExtension()' => [$params['yiisoft/view']['defaultExtension']],
23+
'reset' => function (ContainerInterface $container) use ($params) {
2224
/** @var View $this */
2325
$this->clear();
24-
$this->setParameters($params['yiisoft/view']['parameters']);
26+
$parameters = $params['yiisoft/view']['parameters'];
27+
foreach ($parameters as $name => $parameter) {
28+
$parameters[$name] = $parameter instanceof ReferenceInterface ?
29+
$parameter->resolve($container) :
30+
$parameter;
31+
}
32+
$this->setParameters($parameters);
2533
},
2634
],
2735
];

config/params.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
'basePath' => '',
1212
'baseUrl' => '',
1313
],
14+
'renderers' => [],
15+
'defaultExtension' => 'php',
1416
],
1517
];

docs/basic-functionality.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Basic functionality
22

33
The package provides a `Yiisoft\View\View` class with basic functionality for managing views, and
4-
a `Yiisoft\View\WebView` class with advanced functionality for use in a WEB environment. This guide applies to both
4+
a `Yiisoft\View\WebView` class with advanced functionality for use in a web environment. This guide applies to both
55
classes, but examples will be provided using the `Yiisoft\View\View`. For advanced examples with
6-
`Yiisoft\View\WebView` functionality, see the "[Use in the WEB environment](use-in-web-environment.md)" guide.
6+
`Yiisoft\View\WebView` functionality, see the "[Use in the web environment](use-in-web-environment.md)" guide.
77

88
To create a `Yiisoft\View\View` class, you must specify two mandatory parameters:
99

@@ -43,15 +43,15 @@ Posts:
4343

4444
Within a view, you can access `$this` which refers to the `Yiisoft\View\View` managing and rendering current view template.
4545
Besides `$this`, there may be other variables in a view, such as `$posts` in the above example.
46-
These variables represent the data that is passed as parameters when rendering the view. Note that `<?=`
47-
does not automatically encode variables for safe use with HTML and you should take care of it.
46+
These variables represent the data that's passed as parameters when rendering the view. Note that `<?=`
47+
doesn't automatically encode variables for safe use with HTML and you should take care of it.
4848

49-
> Tip: The predefined variables are listed in a comment block at beginning of a view so that they
50-
> can be recognized by IDEs. It is also a good way of documenting your views.
49+
> Tip: The predefined variables are listed in a comment block at the beginning of a view so that they
50+
> can be recognized by IDEs. It's also a good way of documenting your views.
5151
5252
## Rendering
5353

54-
To render the file shown above, two methods are provided: `render()` and `renderFile()`.
54+
To render the file shown above, there are two methods: `render()` and `renderFile()`.
5555

5656
The `renderFile()` method accepts a full absolute path of the view file to be rendered,
5757
and an array of parameters (name-value pairs) that will be available in the view template:
@@ -75,9 +75,9 @@ Instead of an absolute file path, it accepts a name of a view in one of the foll
7575
- A name of a view starting with a slash (for example, `/blog/posts`). It will be prepended with
7676
the base path that was passed to `Yiisoft\View\View` constructor. For example, `/blog/posts`
7777
will be resolved into `/path/to/views/blog/posts.php`.
78-
- A name of a view without the starting slash (e.g. `blog/posts`). The corresponding view file will be looked for
78+
- A name of a view without the starting slash (such as `blog/posts`). The corresponding view file will be looked for
7979
in the context (instance of `Yiisoft\View\ViewContextInterface`) set via `$view->withContext()`. If the
80-
context instance was not set, it will be looked for under the directory containing the view currently being
80+
context instance wasn't set, it will be looked for under the directory containing the view currently being
8181
rendered.
8282

8383
The view name may omit a file extension. In this case, `.php` will be used as the extension.
@@ -119,11 +119,11 @@ and use that implementation via `$view->withRenderers()` method.
119119
```php
120120
$view = $view->withRenderers([
121121
'tpl' => new MyCustomViewRenderer(),
122-
'twig' => new \Yiisoft\Yii\Twig\ViewRenderer($environment),
122+
'twig' => new \Yiisoft\View\Twig\TemplateRenderer($environment),
123123
]);
124124
```
125125

126-
During rendering, the file extension will be analyzed and if the array key matches the file extension,
126+
During rendering, the file extension will be analyzed, and if the array key matches the file extension,
127127
the corresponding renderer will be applied.
128128

129129
## Theming
@@ -199,7 +199,7 @@ $view = $view->withSourceLocale('es');
199199
$view->setLocale('fr');
200200
```
201201

202-
In order to use multiple locales it is necessary to create subdirectories at directory level matching template files
202+
To use multiple locales, it's necessary to create subdirectories at directory level matching template files
203203
of the view. For example, if there is a view `/path/to/views/blog/posts.php` and we translate it into Russian, create
204204
a subdirectory `ru-RU` or `ru`. In this subdirectory, create a file for the Russian locale:
205205
`/path/to/views/blog/ru/posts.php`.
@@ -231,10 +231,10 @@ $view->localize($file, 'ru', 'ru');
231231
File choice is based on the specified locale code. A file with the same name will be looked
232232
for under the subdirectory whose name is the same as the locale code. For example, given the file
233233
`/path/to/views/blog/posts.php` and the locale code `ru-RU`, the localized file will be looked
234-
for as `/path/to/views/blog/ru-RU/posts.php`. If the file is not found, it will try a fallback
235-
with just a language code that is `ru` i.e. `/path/to/views/blog/ru/posts.php`.
234+
for as `/path/to/views/blog/ru-RU/posts.php`. If the file isn't found, it will try a fallback
235+
with just a language code that's `ru` i.e. `/path/to/views/blog/ru/posts.php`.
236236

237-
> If the target file is not found, the original file will be returned.
237+
> If the target file isn't found, the original file will be returned.
238238
> If the target and the source locale codes are the same, the original file will be returned.
239239
240240
## Sharing data among views
@@ -254,7 +254,7 @@ $this->setBlock('block-id-1', '...content of block1...');
254254
$this->setBlock('block-id-2', '...content of block2...');
255255
```
256256

257-
Then, display the blocks if there are any, or the default content if the block is not defined:
257+
Then, display the blocks if there are any, or the default content if the block isn't defined:
258258

259259
```php
260260
<?php
@@ -321,7 +321,7 @@ Posts:
321321
```
322322

323323
You can not call the `hasParameter()` method, but pass the default value to the `getParameter()` method.
324-
At the same time, if the default value is not passed, and the requested parameter does not exist,
324+
At the same time, if the default value isn't passed, and the requested parameter doesn't exist,
325325
an `InvalidArgumentException` exception will be thrown.
326326

327327
```php
@@ -336,7 +336,7 @@ To delete data, use `removeBlock('id')` and `removeParameter('id')` methods.
336336

337337
## Content caching
338338

339-
In some cases, caching content can significantly improve performance of your application. For example,
339+
Sometimes, caching content can significantly improve the performance of your application. For example,
340340
if a page displays a summary of yearly sales in a table, you can store this table in a cache to eliminate
341341
the time needed to generate this table for each request.
342342

@@ -356,7 +356,7 @@ $cachedContent = new CachedContent('cache-id', $cache);
356356
// Trying to get content from the cache
357357
$content = $cachedContent->get();
358358

359-
// If the content is not in the cache, then we will generate it and add it to the cache
359+
// If the content isn't in the cache, then we will generate it and add it to the cache
360360
if ($content === null) {
361361
// Generating content
362362
$content = $view->render('view/name');
@@ -369,21 +369,21 @@ echo $content;
369369
```
370370

371371
In addition to the content, the `Yiisoft\View\Cache\CachedContent::cache()` method
372-
accepts three additional optional arguments:
372+
accepts three extra optional arguments:
373373

374374
- `$ttl (int)` - The TTL of the cached content. Default is `60`.
375375
- `$dependency (Yiisoft\Cache\Dependency\Dependency|null)` - The dependency of the cached content. Default is `null`.
376-
- `$beta (float)` - The value for calculating the range that is used for "Probably early expiration". Default is `1.0`.
376+
- `$beta (float)` - The value for calculating the range that's used for "Probably early expiration". Default is `1.0`.
377377

378378
For more information about caching and cache options, see the documentation of the
379379
[yiisoft/cache package](https://github.com/yiisoft/cache).
380380

381381
### Dynamic Content
382382

383-
When caching content, you may encounter the situation where a large fragment of content is relatively
383+
When caching content, you may meet the situation where a large fragment of content is relatively
384384
static except one or a few places. For example, a page header may display a main menu bar together with
385385
a name of the current user. Another problem is that the content being cached may contain PHP code that must
386-
be executed for every request. Both problems can be solved by using the `Yiisoft\View\Cache\DynamicContent` class.
386+
be executed for every request. You can solve both problems by using the `Yiisoft\View\Cache\DynamicContent` class.
387387

388388
```php
389389
/**
@@ -409,7 +409,7 @@ $cachedContent = new CachedContent('cache-id', $cache, [$dynamicContent]);
409409
// Trying to get content from the cache
410410
$content = $cachedContent->get();
411411

412-
// If the content is not in the cache, then we will generate it and add it to the cache
412+
// If the content isn't in the cache, then we will generate it and add it to the cache
413413
if ($content === null) {
414414
// Generating content
415415
// In the view, we call `$dynamicContent->placeholder()`
@@ -422,7 +422,7 @@ if ($content === null) {
422422
echo $content;
423423
```
424424

425-
A dynamic content means a fragment of output that should not be cached even if it is enclosed within a fragment cache.
425+
A dynamic content means a fragment of output that shouldn't be cached even if it's enclosed within a fragment cache.
426426
You may call `$dynamicContent->placeholder()` within a cached fragment to insert dynamic content at the desired place
427427
of the view, like the following:
428428

@@ -445,7 +445,7 @@ Content to be cached ...
445445
```
446446

447447

448-
For caching content fragments, it is much more convenient to use dynamic content using the
448+
For caching content fragments, it's much more convenient to use dynamic content using the
449449
`Yiisoft\Yii\Widgets\FragmentCache` widget from the
450450
[yiisoft/yii-widgets](https://github.com/yiisoft/yii-widgets) package:
451451

@@ -475,7 +475,7 @@ FragmentCache::end();
475475

476476
### Variations
477477

478-
Content being cached may be varied according to some parameters. For example, for a Web application supporting
478+
Content being cached may be varied according to some parameters. For example, for a web application supporting
479479
multiple locales, the same piece of view code may generate the content in different locales. Therefore, you
480480
may want to make the cached content varied according to the current application locale.
481481

0 commit comments

Comments
 (0)