ContentDecorator.php 1.21 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

Qiang Xue committed
8 9 10 11 12
namespace yii\widgets;

use yii\base\InvalidConfigException;
use yii\base\Widget;

Qiang Xue committed
13 14
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
15
 * @since 2.0
Qiang Xue committed
16
 */
Qiang Xue committed
17
class ContentDecorator extends Widget
Qiang Xue committed
18 19
{
	/**
Qiang Xue committed
20 21
	 * @var string the view file that will be used to decorate the content enclosed by this widget.
	 * This can be specified as either the view file path or path alias.
Qiang Xue committed
22
	 */
Qiang Xue committed
23
	public $viewFile;
Qiang Xue committed
24
	/**
resurtm committed
25
	 * @var array the parameters (name => value) to be extracted and made available in the decorative view.
Qiang Xue committed
26
	 */
Alexander Makarov committed
27
	public $params = [];
Qiang Xue committed
28 29

	/**
Qiang Xue committed
30
	 * Starts recording a clip.
Qiang Xue committed
31
	 */
Qiang Xue committed
32
	public function init()
Qiang Xue committed
33
	{
Qiang Xue committed
34 35
		if ($this->viewFile === null) {
			throw new InvalidConfigException('ContentDecorator::viewFile must be set.');
Qiang Xue committed
36 37 38
		}
		ob_start();
		ob_implicit_flush(false);
Qiang Xue committed
39 40 41
	}

	/**
Qiang Xue committed
42 43
	 * Ends recording a clip.
	 * This method stops output buffering and saves the rendering result as a named clip in the controller.
Qiang Xue committed
44
	 */
Qiang Xue committed
45
	public function run()
Qiang Xue committed
46
	{
Qiang Xue committed
47 48
		$params = $this->params;
		$params['content'] = ob_get_clean();
Qiang Xue committed
49 50
		// render under the existing context
		echo $this->view->renderFile($this->viewFile, $params);
Qiang Xue committed
51 52
	}
}