ContentDecorator.php 1.42 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 13 14
namespace yii\widgets;

use Yii;
use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\base\View;

Qiang Xue committed
15 16
/**
 * @author Qiang Xue <qiang.xue@gmail.com>
Qiang Xue committed
17
 * @since 2.0
Qiang Xue committed
18
 */
Qiang Xue committed
19
class ContentDecorator extends Widget
Qiang Xue committed
20 21
{
	/**
Qiang Xue committed
22 23
	 * @var View the view object for rendering [[viewName]]. If not set, the view registered with the application
	 * will be used.
Qiang Xue committed
24 25 26
	 */
	public $view;
	/**
Qiang Xue committed
27 28
	 * @var string the name of the view that will be used to decorate the content enclosed by this widget.
	 * Please refer to [[View::findViewFile()]] on how to set this property.
Qiang Xue committed
29
	 */
Qiang Xue committed
30 31 32 33 34
	public $viewName;
	/**
	 * @var array the parameters (name=>value) to be extracted and made available in the decorative view.
	 */
	public $params = array();
Qiang Xue committed
35 36

	/**
Qiang Xue committed
37
	 * Starts recording a clip.
Qiang Xue committed
38
	 */
Qiang Xue committed
39
	public function init()
Qiang Xue committed
40
	{
Qiang Xue committed
41 42 43 44 45
		if ($this->viewName === null) {
			throw new InvalidConfigException('ContentDecorator::viewName must be set.');
		}
		ob_start();
		ob_implicit_flush(false);
Qiang Xue committed
46 47 48
	}

	/**
Qiang Xue committed
49 50
	 * Ends recording a clip.
	 * This method stops output buffering and saves the rendering result as a named clip in the controller.
Qiang Xue committed
51
	 */
Qiang Xue committed
52
	public function run()
Qiang Xue committed
53
	{
Qiang Xue committed
54 55 56 57
		$params = $this->params;
		$params['content'] = ob_get_clean();
		$view = $this->view !== null ? $this->view : Yii::$app->getView();
		echo $view->render($this->viewName, $params);
Qiang Xue committed
58 59
	}
}