11# Basic functionality
22
33The 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
55classes, 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
88To create a ` Yiisoft\View\View ` class, you must specify two mandatory parameters:
99
@@ -43,15 +43,15 @@ Posts:
4343
4444Within a view, you can access ` $this ` which refers to the ` Yiisoft\View\View ` managing and rendering current view template.
4545Besides ` $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
5656The ` renderFile() ` method accepts a full absolute path of the view file to be rendered,
5757and 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
8383The 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,
127127the 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
203203of the view. For example, if there is a view ` /path/to/views/blog/posts.php ` and we translate it into Russian, create
204204a 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');
231231File choice is based on the specified locale code. A file with the same name will be looked
232232for 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
323323You 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,
325325an ` 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,
340340if a page displays a summary of yearly sales in a table, you can store this table in a cache to eliminate
341341the 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
360360if ($content === null) {
361361 // Generating content
362362 $content = $view->render('view/name');
@@ -369,21 +369,21 @@ echo $content;
369369```
370370
371371In 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
378378For 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
384384static except one or a few places. For example, a page header may display a main menu bar together with
385385a 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
413413if ($content === null) {
414414 // Generating content
415415 // In the view, we call `$dynamicContent->placeholder()`
@@ -422,7 +422,7 @@ if ($content === null) {
422422echo $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.
426426You may call ` $dynamicContent->placeholder() ` within a cached fragment to insert dynamic content at the desired place
427427of 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
479479multiple locales, the same piece of view code may generate the content in different locales. Therefore, you
480480may want to make the cached content varied according to the current application locale.
481481
0 commit comments