|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Widgets; |
|
6
|
|
|
|
|
7
|
|
|
use Throwable; |
|
8
|
|
|
use Yiisoft\Aliases\Aliases; |
|
9
|
|
|
use Yiisoft\View\Exception\ViewNotFoundException; |
|
10
|
|
|
use Yiisoft\View\WebView; |
|
11
|
|
|
use Yiisoft\Widget\Widget; |
|
12
|
|
|
|
|
13
|
|
|
use function ob_get_clean; |
|
14
|
|
|
use function ob_start; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ContentDecorator records all output between {@see Widget::begin()} and {@see Widget::end()} calls, |
|
18
|
|
|
* passes it to the given view file as `$content` and then echoes rendering result. |
|
19
|
|
|
* |
|
20
|
|
|
* ```php |
|
21
|
|
|
* <?= ContentDecorator::widget() |
|
22
|
|
|
* ->viewFile('@app/views/layouts/base.php') |
|
23
|
|
|
* ->parameters([]) |
|
24
|
|
|
* ->begin(); |
|
25
|
|
|
* ?> |
|
26
|
|
|
* |
|
27
|
|
|
* some content here |
|
28
|
|
|
* |
|
29
|
|
|
* <?= ContentDecorator::end() ?> |
|
30
|
|
|
* ``` |
|
31
|
|
|
*/ |
|
32
|
|
|
final class ContentDecorator extends Widget |
|
33
|
|
|
{ |
|
34
|
|
|
private array $parameters = []; |
|
35
|
|
|
private string $viewFile = ''; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(private Aliases $aliases, private WebView $webView) |
|
38
|
|
|
{ |
|
39
|
|
|
} |
|
40
|
2 |
|
|
|
41
|
|
|
/** |
|
42
|
2 |
|
* Returns a new instance with the specified parameters. |
|
43
|
2 |
|
* |
|
44
|
|
|
* @param array $value The parameters (name => value) to be extracted and made available in the decorative view. |
|
45
|
|
|
*/ |
|
46
|
|
|
public function parameters(array $value): self |
|
47
|
|
|
{ |
|
48
|
|
|
$new = clone $this; |
|
49
|
|
|
$new->parameters = $value; |
|
50
|
|
|
|
|
51
|
|
|
return $new; |
|
52
|
|
|
} |
|
53
|
1 |
|
|
|
54
|
|
|
/** |
|
55
|
1 |
|
* Returns a new instance with the specified view file. |
|
56
|
1 |
|
* |
|
57
|
1 |
|
* @param string $value The view file that will be used to decorate the content enclosed by this widget. |
|
58
|
|
|
* This can be specified as either the view file path or alias path. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function viewFile(string $value): self |
|
61
|
|
|
{ |
|
62
|
|
|
$new = clone $this; |
|
63
|
|
|
$new->viewFile = $this->aliases->get($value); |
|
64
|
|
|
|
|
65
|
|
|
return $new; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
/** |
|
69
|
|
|
* Starts recording a content. |
|
70
|
2 |
|
*/ |
|
71
|
2 |
|
public function begin(): ?string |
|
72
|
2 |
|
{ |
|
73
|
|
|
parent::begin(); |
|
74
|
|
|
|
|
75
|
|
|
ob_start(); |
|
76
|
|
|
|
|
77
|
|
|
return null; |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
/** |
|
81
|
1 |
|
* Ends recording a content. |
|
82
|
|
|
* |
|
83
|
1 |
|
* This method stops output buffering and saves the rendering result as a `$content` |
|
84
|
1 |
|
* variable and then echoes rendering result. |
|
85
|
|
|
* |
|
86
|
|
|
* @throws Throwable|ViewNotFoundException |
|
87
|
|
|
* |
|
88
|
|
|
* @return string The result of widget execution to be outputted. |
|
89
|
|
|
*/ |
|
90
|
|
|
public function render(): string |
|
91
|
|
|
{ |
|
92
|
|
|
$parameters = $this->parameters; |
|
93
|
|
|
$parameters['content'] = ob_get_clean(); |
|
94
|
|
|
|
|
95
|
|
|
/** render under the existing context */ |
|
96
|
|
|
return $this->webView->render($this->viewFile, $parameters); |
|
97
|
1 |
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|