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