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