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