Widget.php 3.19 KB
Newer Older
Qiang Xue committed
1 2 3 4 5
<?php
/**
 * Widget class file.
 *
 * @link http://www.yiiframework.com/
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008 Yii Software LLC
Qiang Xue committed
7 8 9 10 11 12 13 14 15 16 17
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

/**
 * Widget is the base class for widgets.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
18
class Widget extends Component
Qiang Xue committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
	/**
	 * @var Widget|Controller the owner/creator of this widget. It could be either a widget or a controller.
	 */
	public $owner;
	/**
	 * @var string id of the widget.
	 */
	private $_id;
	/**
	 * @var integer a counter used to generate IDs for widgets.
	 */
	private static $_counter = 0;

	/**
	 * Constructor.
	 * @param Widget|Controller $owner owner/creator of this widget.
Qiang Xue committed
36
	 * @param array $config name-value pairs that will be used to initialize the object properties
Qiang Xue committed
37
	 */
Qiang Xue committed
38
	public function __construct($owner, $config = array())
Qiang Xue committed
39 40
	{
		$this->owner = $owner;
Qiang Xue committed
41
		parent::__construct($config);
Qiang Xue committed
42 43 44 45 46 47 48 49 50 51
	}

	/**
	 * Returns the ID of the widget.
	 * @param boolean $autoGenerate whether to generate an ID if it is not set previously
	 * @return string ID of the widget.
	 */
	public function getId($autoGenerate = true)
	{
		if ($autoGenerate && $this->_id === null) {
Qiang Xue committed
52
			$this->_id = 'w' . self::$_counter++;
Qiang Xue committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
		}
		return $this->_id;
	}

	/**
	 * Sets the ID of the widget.
	 * @param string $value id of the widget.
	 */
	public function setId($value)
	{
		$this->_id = $value;
	}

	/**
	 * Executes the widget.
	 */
	public function run()
	{
	}

	/**
	 * Renders a view.
	 *
	 * The method first finds the actual view file corresponding to the specified view.
	 * It then calls [[renderFile()]] to render the view file. The rendering result is returned
	 * as a string. If the view file does not exist, an exception will be thrown.
	 *
	 * To determine which view file should be rendered, the method calls [[findViewFile()]] which
	 * will search in the directories as specified by [[basePath]].
	 *
Qiang Xue committed
83
	 * View name can be a path alias representing an absolute file path (e.g. `@application/views/layout/index`),
Qiang Xue committed
84 85 86 87 88 89 90 91 92 93 94
	 * or a path relative to [[basePath]]. The file suffix is optional and defaults to `.php` if not given
	 * in the view name.
	 *
	 * @param string $view the view to be rendered. This can be either a path alias or a path relative to [[basePath]].
	 * @param array $params the parameters that should be made available in the view. The PHP function `extract()`
	 * will be called on this variable to extract the variables from this parameter.
	 * @return string the rendering result
	 * @throws Exception if the view file cannot be found
	 */
	public function render($view, $params = array())
	{
Qiang Xue committed
95
		return $this->createView()->renderPartial($view, $params);
Qiang Xue committed
96 97 98 99 100 101 102
	}

	/**
	 * @return View
	 */
	public function createView()
	{
Qiang Xue committed
103
		return new View($this);
Qiang Xue committed
104
	}
Qiang Xue committed
105 106 107 108 109 110 111 112 113 114 115 116

	/**
	 * Returns the directory containing the view files for this widget.
	 * The default implementation returns the 'views' subdirectory under the directory containing the widget class file.
	 * @return string the directory containing the view files for this widget.
	 */
	public function getViewPath()
	{
		$className = get_class($this);
		$class = new \ReflectionClass($className);
		return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
	}
Qiang Xue committed
117
}