Skip to content

Commit 8f325bc

Browse files
authored
Add guide dummy on classic frontend (#242)
1 parent 0abb66a commit 8f325bc

File tree

7 files changed

+154
-6
lines changed

7 files changed

+154
-6
lines changed

guide/en/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ Views -
5959
-----
6060

6161
* [Views](views/view.md) -
62-
* [Widgets](views/widget.md) -
63-
* [Assets](views/asset.md) -
64-
* [Working with client scripts](views/client-scripts.md) -
65-
* [Theming](views/theming.md) -
6662
* [Template engines](views/template-engines.md) -
6763
* [View injections](views/view-injections.md) +
68-
64+
* [Scripts, styles and metatags](views/script-style-meta.md)
65+
* [Assets](views/asset.md) -
66+
* [Widgets](views/widget.md) -
6967

7068
Working with databases +-
7169
----------------------

guide/en/views/asset.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Assets
2+
3+
> [!NOTE]
4+
> [← Scripts, styles and metatags](script-style-meta.md) |
5+
> [Widgets →](widget.md)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Scripts, styles and metatags
2+
3+
> [!NOTE]
4+
> [← View injections](view-injections.md) |
5+
> [Assets →](asset.md)

guide/en/views/template-engines.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Template engines
2+
3+
Yii views support multiple template engines as well as custom engines. By default, Yii uses the PHP engine.
4+
Additionally, [Twig](https://twig.symfony.com/) is available.
5+
6+
## PHP
7+
8+
PHP engine is available by default. The following is the basic template syntax:
9+
10+
```php
11+
<?php
12+
13+
declare(strict_types=1);
14+
15+
use Yiisoft\Html\Html;
16+
17+
/**
18+
* @var App\Blog\Post[] $posts
19+
*/
20+
?>
21+
22+
Posts:
23+
24+
<dl>
25+
<?php foreach ($posts as $post): ?>
26+
<dt>Title: <?= Html::encode($post->getTitle()) ?></dt>
27+
<dd>Description: <?= Html::encode($post->getDescription()) ?></dd>
28+
<?php endforeach; ?>
29+
</dl>
30+
```
31+
32+
At the very top of the template, you can define "uses" for classes to use and declare the type of variables
33+
so the IDE understands them. The rest is the PHP code.
34+
35+
> [!WARNING]
36+
> `<?=` or `echo` doesn't automatically encode variables for safe use with HTML and you should take care of it by using
37+
> `Html::encode()`.
38+
39+
If you need a sub-view, you can use it like this:
40+
41+
```php
42+
<?php
43+
44+
declare(strict_types=1);
45+
46+
/**
47+
* @var Yiisoft\View\View $this
48+
* @var App\Blog\Post[] $posts
49+
*/
50+
?>
51+
52+
Title
53+
54+
<?= $this->render('blog/posts', ['posts' => $posts]) ?>
55+
```
56+
57+
### See also
58+
59+
- [yiisoft/view docs](https://github.com/yiisoft/view/blob/master/docs/guide/en/README.md).
60+
-
61+
62+
## Twig
63+
64+
To use Twig, you need to install the [yiisoft/yii-twig](https://github.com/yiisoft/yii-twig).
65+
66+
67+
> [!NOTE]
68+
> [← Views](view.md) |
69+
> [View injections →](view-injections.md)

guide/en/views/view-injections.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# View injections
22

33
The view injections are designed to provide a standardized way to pass parameters to the common layer
4-
of views in an application. Implementing this interface allows developers to manage the data that will be available
4+
of views in an application. It allows developers to manage the data that will be available
55
across various views, ensuring flexibility and reusability of code.
66

7+
The view injections could be used if you require `yiisoft/yii-view-renderer` package:
8+
9+
10+
```sh
11+
composer require yiisoft/yii-view-renderer
12+
```
13+
714
## Configuration
815

916
In config `params.php`:
@@ -101,3 +108,7 @@ Add your new injection to `params.php` under specific layout name. In the follow
101108
],
102109
],
103110
```
111+
112+
> [!NOTE]
113+
> [← Template engines](template-engines.md) |
114+
> [Scripts, styles and metatags →](script-style-meta.md)

guide/en/views/view.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Views
2+
3+
Yii3 views could be used by requiring the `yiisoft/view` package:
4+
5+
```sh
6+
composer require yiisoft/view
7+
```
8+
9+
The package provides template rendering abstraction supporting layout-view-subview hierarchy, custom renderers
10+
with PHP-based as default, and more.
11+
12+
Usage is the following:
13+
14+
```php
15+
```
16+
17+
If you're building a web application, you should require the `yiisoft/yii-view-renderer` package as well:
18+
19+
```sh
20+
composer require yiisoft/yii-view-renderer
21+
```
22+
23+
It provides some extra web-specific functionality and adds compatibility with PSR-7 interfaces and could be used like
24+
the following:
25+
26+
```php
27+
<?php
28+
29+
declare(strict_types=1);
30+
31+
namespace App\Controller\HomePage;
32+
33+
use Psr\Http\Message\ResponseInterface;
34+
use Yiisoft\Yii\View\Renderer\ViewRenderer;
35+
36+
final readonly class Action
37+
{
38+
public function __construct(
39+
private ViewRenderer $viewRenderer,
40+
) {}
41+
42+
public function __invoke(): ResponseInterface
43+
{
44+
return $this->viewRenderer->render(__DIR__ . '/template', [
45+
'name' => 'Sergei',
46+
'surname' => 'Predvoditelev',
47+
]);
48+
}
49+
}
50+
```
51+
52+
In both cases a template file should be supplied. The template syntax depends on the template engine used.
53+
54+
> [!NOTE]
55+
> [Template engines →](template-engines.md)

guide/en/views/widget.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Widgets
2+
3+
4+
> [!NOTE]
5+
> [← Assets](asset.md)

0 commit comments

Comments
 (0)