1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\widgets; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidConfigException; |
11
|
|
|
use yii\base\Widget; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ContentDecorator records all output between [[begin()]] and [[end()]] calls, passes it to the given view file |
15
|
|
|
* as `$content` and then echoes rendering result. |
16
|
|
|
* |
17
|
|
|
* ```php |
18
|
|
|
* <?php ContentDecorator::begin([ |
19
|
|
|
* 'viewFile' => '@app/views/layouts/base.php', |
20
|
|
|
* 'params' => [], |
21
|
|
|
* 'view' => $this, |
22
|
|
|
* ]) ?> |
23
|
|
|
* |
24
|
|
|
* some content here |
25
|
|
|
* |
26
|
|
|
* <?php ContentDecorator::end() ?> |
27
|
|
|
* ``` |
28
|
|
|
* |
29
|
|
|
* There are [[\yii\base\View::beginContent()]] and [[\yii\base\View::endContent()]] wrapper methods in the |
30
|
|
|
* [[\yii\base\View]] component to make syntax more friendly. In the view these could be used as follows: |
31
|
|
|
* |
32
|
|
|
* ```php |
33
|
|
|
* <?php $this->beginContent('@app/views/layouts/base.php') ?> |
34
|
|
|
* |
35
|
|
|
* some content here |
36
|
|
|
* |
37
|
|
|
* <?php $this->endContent() ?> |
38
|
|
|
* ``` |
39
|
|
|
* |
40
|
|
|
* @author Qiang Xue <[email protected]> |
41
|
|
|
* @since 2.0 |
42
|
|
|
*/ |
43
|
|
|
class ContentDecorator extends Widget |
44
|
|
|
{ |
45
|
|
|
/** |
46
|
|
|
* @var string the view file that will be used to decorate the content enclosed by this widget. |
47
|
|
|
* This can be specified as either the view file path or [path alias](guide:concept-aliases). |
48
|
|
|
*/ |
49
|
|
|
public $viewFile; |
50
|
|
|
/** |
51
|
|
|
* @var array the parameters (name => value) to be extracted and made available in the decorative view. |
52
|
|
|
*/ |
53
|
|
|
public $params = []; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Starts recording a clip. |
58
|
|
|
*/ |
59
|
1 |
|
public function init() |
60
|
|
|
{ |
61
|
1 |
|
parent::init(); |
62
|
|
|
|
63
|
1 |
|
if ($this->viewFile === null) { |
64
|
|
|
throw new InvalidConfigException('ContentDecorator::viewFile must be set.'); |
65
|
|
|
} |
66
|
1 |
|
ob_start(); |
67
|
1 |
|
ob_implicit_flush(false); |
|
|
|
|
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Ends recording a clip. |
72
|
|
|
* This method stops output buffering and saves the rendering result as a named clip in the controller. |
73
|
|
|
*/ |
74
|
|
|
public function run() |
75
|
|
|
{ |
76
|
|
|
$params = $this->params; |
77
|
|
|
$params['content'] = ob_get_clean(); |
78
|
|
|
// render under the existing context |
79
|
|
|
echo $this->view->renderFile($this->viewFile, $params); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|