View.php 14.8 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 8 9
 * @license http://www.yiiframework.com/license/
 */

namespace yii\base;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\helpers\FileHelper;
12 13 14
use yii\widgets\Block;
use yii\widgets\ContentDecorator;
use yii\widgets\FragmentCache;
Qiang Xue committed
15

Qiang Xue committed
16
/**
Qiang Xue committed
17
 * View represents a view object in the MVC pattern.
Qiang Xue committed
18
 *
Qiang Xue committed
19
 * View provides a set of methods (e.g. [[render()]]) for rendering purpose.
Qiang Xue committed
20
 *
Qiang Xue committed
21 22 23 24 25
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class View extends Component
{
26
	/**
Qiang Xue committed
27
	 * @event Event an event that is triggered by [[beginPage()]].
28 29 30
	 */
	const EVENT_BEGIN_PAGE = 'beginPage';
	/**
Qiang Xue committed
31
	 * @event Event an event that is triggered by [[endPage()]].
32 33
	 */
	const EVENT_END_PAGE = 'endPage';
34
	/**
35
	 * @event ViewEvent an event that is triggered by [[renderFile()]] right before it renders a view file.
36 37 38
	 */
	const EVENT_BEFORE_RENDER = 'beforeRender';
	/**
39
	 * @event ViewEvent an event that is triggered by [[renderFile()]] right after it renders a view file.
40 41 42
	 */
	const EVENT_AFTER_RENDER = 'afterRender';

43
	/**
44
	 * @var ViewContextInterface the context under which the [[renderFile()]] method is being invoked.
45 46
	 */
	public $context;
Qiang Xue committed
47
	/**
Qiang Xue committed
48
	 * @var mixed custom parameters that are shared among view templates.
Qiang Xue committed
49
	 */
Alexander Makarov committed
50
	public $params = [];
Qiang Xue committed
51
	/**
Qiang Xue committed
52 53
	 * @var array a list of available renderers indexed by their corresponding supported file extensions.
	 * Each renderer may be a view renderer object or the configuration for creating the renderer object.
54 55 56
	 * For example, the following configuration enables both Smarty and Twig view renderers:
	 *
	 * ~~~
Alexander Makarov committed
57 58 59 60
	 * [
	 *     'tpl' => ['class' => 'yii\smarty\ViewRenderer'],
	 *     'twig' => ['class' => 'yii\twig\ViewRenderer'],
	 * ]
61
	 * ~~~
Qiang Xue committed
62 63 64
	 *
	 * If no renderer is available for the given view file, the view file will be treated as a normal PHP
	 * and rendered via [[renderPhpFile()]].
Qiang Xue committed
65
	 */
66
	public $renderers;
67 68 69 70
	/**
	 * @var string the default view file extension. This will be appended to view file names if they don't have file extensions.
	 */
	public $defaultExtension = '.php';
71
	/**
72
	 * @var Theme|array the theme object or the configuration array for creating the theme object.
Qiang Xue committed
73
	 * If not set, it means theming is not enabled.
74
	 */
Qiang Xue committed
75
	public $theme;
Qiang Xue committed
76
	/**
Qiang Xue committed
77 78
	 * @var array a list of named output blocks. The keys are the block names and the values
	 * are the corresponding block content. You can call [[beginBlock()]] and [[endBlock()]]
79
	 * to capture small fragments of a view. They can be later accessed somewhere else
Qiang Xue committed
80 81
	 * through this property.
	 */
Qiang Xue committed
82
	public $blocks;
Qiang Xue committed
83 84
	/**
	 * @var array a list of currently active fragment cache widgets. This property
85 86
	 * is used internally to implement the content caching feature. Do not modify it directly.
	 * @internal
Qiang Xue committed
87
	 */
Alexander Makarov committed
88
	public $cacheStack = [];
Qiang Xue committed
89 90
	/**
	 * @var array a list of placeholders for embedding dynamic contents. This property
91 92
	 * is used internally to implement the content caching feature. Do not modify it directly.
	 * @internal
Qiang Xue committed
93
	 */
Alexander Makarov committed
94
	public $dynamicPlaceholders = [];
Qiang Xue committed
95 96


Qiang Xue committed
97
	/**
Qiang Xue committed
98
	 * Initializes the view component.
Qiang Xue committed
99
	 */
Qiang Xue committed
100
	public function init()
Qiang Xue committed
101
	{
Qiang Xue committed
102 103
		parent::init();
		if (is_array($this->theme)) {
104 105 106
			if (!isset($this->theme['class'])) {
				$this->theme['class'] = 'yii\base\Theme';
			}
Qiang Xue committed
107
			$this->theme = Yii::createObject($this->theme);
Qiang Xue committed
108 109 110
		}
	}

Qiang Xue committed
111
	/**
Qiang Xue committed
112
	 * Renders a view.
Qiang Xue committed
113
	 *
114
	 * The view to be rendered can be specified in one of the following formats:
Qiang Xue committed
115
	 *
116 117 118 119 120 121
	 * - path alias (e.g. "@app/views/site/index");
	 * - absolute path within application (e.g. "//site/index"): the view name starts with double slashes.
	 *   The actual view file will be looked for under the [[Application::viewPath|view path]] of the application.
	 * - absolute path within current module (e.g. "/site/index"): the view name starts with a single slash.
	 *   The actual view file will be looked for under the [[Module::viewPath|view path]] of [[module]].
	 * - resolving any other format will be performed via [[ViewContext::findViewFile()]].
Qiang Xue committed
122 123 124
	 *
	 * @param string $view the view name. Please refer to [[Controller::findViewFile()]]
	 * and [[Widget::findViewFile()]] on how to specify this parameter.
Qiang Xue committed
125
	 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
126 127
	 * @param object $context the context that the view should use for rendering the view. If null,
	 * existing [[context]] will be used.
128
	 * @return string the rendering result
Qiang Xue committed
129
	 * @throws InvalidParamException if the view cannot be resolved or the view file does not exist.
130
	 * @see renderFile()
Qiang Xue committed
131
	 */
132
	public function render($view, $params = [], $context = null)
Qiang Xue committed
133
	{
134 135 136 137 138 139 140 141 142 143 144
		$viewFile = $this->findViewFile($view, $context);
		return $this->renderFile($viewFile, $params, $context);
	}

	/**
	 * Finds the view file based on the given view name.
	 * @param string $view the view name or the path alias of the view file. Please refer to [[render()]]
	 * on how to specify this parameter.
	 * @param object $context the context that the view should be used to search the view file. If null,
	 * existing [[context]] will be used.
	 * @return string the view file path. Note that the file may not exist.
145
	 * @throws InvalidCallException if [[context]] is required and invalid.
146 147 148 149 150 151 152 153 154
	 */
	protected function findViewFile($view, $context = null)
	{
		if (strncmp($view, '@', 1) === 0) {
			// e.g. "@app/views/main"
			$file = Yii::getAlias($view);
		} elseif (strncmp($view, '//', 2) === 0) {
			// e.g. "//layouts/main"
			$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
155
		} elseif (strncmp($view, '/', 1) === 0) {
156
			// e.g. "/site/index"
157 158 159 160 161
			if (Yii::$app->controller !== null) {
				$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
			} else {
				throw new InvalidCallException("Unable to locate view file for view '$view': no active controller.");
			}
Qiang Xue committed
162
		} else {
163 164 165 166
			// context required
			if ($context === null) {
				$context = $this->context;
			}
167
			if ($context instanceof ViewContextInterface) {
168 169
				$file = $context->findViewFile($view);
			} else {
170
				throw new InvalidCallException("Unable to locate view file for view '$view': no active view context.");
171
			}
Qiang Xue committed
172
		}
173

174
		return pathinfo($file, PATHINFO_EXTENSION) === '' ? $file . $this->defaultExtension : $file;
Qiang Xue committed
175 176
	}

Qiang Xue committed
177 178
	/**
	 * Renders a view file.
Qiang Xue committed
179
	 *
Qiang Xue committed
180 181
	 * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long
	 * as it is available.
Qiang Xue committed
182
	 *
Qiang Xue committed
183 184 185 186 187 188
	 * The method will call [[FileHelper::localize()]] to localize the view file.
	 *
	 * If [[renderer]] is enabled (not null), the method will use it to render the view file.
	 * Otherwise, it will simply include the view file as a normal PHP file, capture its output and
	 * return it as a string.
	 *
Qiang Xue committed
189
	 * @param string $viewFile the view file. This can be either a file path or a path alias.
Qiang Xue committed
190
	 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
Qiang Xue committed
191 192
	 * @param object $context the context that the view should use for rendering the view. If null,
	 * existing [[context]] will be used.
Qiang Xue committed
193
	 * @return string the rendering result
Qiang Xue committed
194
	 * @throws InvalidParamException if the view file does not exist
Qiang Xue committed
195
	 */
Alexander Makarov committed
196
	public function renderFile($viewFile, $params = [], $context = null)
Qiang Xue committed
197
	{
Qiang Xue committed
198
		$viewFile = Yii::getAlias($viewFile);
199 200 201
		if ($this->theme !== null) {
			$viewFile = $this->theme->applyTo($viewFile);
		}
Qiang Xue committed
202 203 204 205 206 207
		if (is_file($viewFile)) {
			$viewFile = FileHelper::localize($viewFile);
		} else {
			throw new InvalidParamException("The view file does not exist: $viewFile");
		}

Qiang Xue committed
208
		$oldContext = $this->context;
Qiang Xue committed
209 210 211
		if ($context !== null) {
			$this->context = $context;
		}
Qiang Xue committed
212

213 214
		$output = '';
		if ($this->beforeRender($viewFile)) {
Qiang Xue committed
215
			Yii::trace("Rendering view file: $viewFile", __METHOD__);
Qiang Xue committed
216 217
			$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
			if (isset($this->renderers[$ext])) {
Qiang Xue committed
218
				if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
Qiang Xue committed
219 220 221 222 223
					$this->renderers[$ext] = Yii::createObject($this->renderers[$ext]);
				}
				/** @var ViewRenderer $renderer */
				$renderer = $this->renderers[$ext];
				$output = $renderer->render($this, $viewFile, $params);
224 225 226 227
			} else {
				$output = $this->renderPhpFile($viewFile, $params);
			}
			$this->afterRender($viewFile, $output);
Qiang Xue committed
228
		}
Qiang Xue committed
229 230 231 232

		$this->context = $oldContext;

		return $output;
Qiang Xue committed
233 234
	}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	/**
	 * This method is invoked right before [[renderFile()]] renders a view file.
	 * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
	 * If you override this method, make sure you call the parent implementation first.
	 * @param string $viewFile the view file to be rendered
	 * @return boolean whether to continue rendering the view file.
	 */
	public function beforeRender($viewFile)
	{
		$event = new ViewEvent($viewFile);
		$this->trigger(self::EVENT_BEFORE_RENDER, $event);
		return $event->isValid;
	}

	/**
	 * This method is invoked right after [[renderFile()]] renders a view file.
	 * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
	 * If you override this method, make sure you call the parent implementation first.
	 * @param string $viewFile the view file to be rendered
	 * @param string $output the rendering result of the view file. Updates to this parameter
	 * will be passed back and returned by [[renderFile()]].
	 */
	public function afterRender($viewFile, &$output)
	{
		if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
			$event = new ViewEvent($viewFile);
			$event->output = $output;
			$this->trigger(self::EVENT_AFTER_RENDER, $event);
			$output = $event->output;
		}
	}

Qiang Xue committed
267
	/**
Qiang Xue committed
268 269 270 271 272 273
	 * Renders a view file as a PHP script.
	 *
	 * This method treats the view file as a PHP script and includes the file.
	 * It extracts the given parameters and makes them available in the view file.
	 * The method captures the output of the included view file and returns it as a string.
	 *
Qiang Xue committed
274 275
	 * This method should mainly be called by view renderer or [[renderFile()]].
	 *
Qiang Xue committed
276 277 278
	 * @param string $_file_ the view file.
	 * @param array $_params_ the parameters (name-value pairs) that will be extracted and made available in the view file.
	 * @return string the rendering result
Qiang Xue committed
279
	 */
Alexander Makarov committed
280
	public function renderPhpFile($_file_, $_params_ = [])
Qiang Xue committed
281
	{
Qiang Xue committed
282 283 284 285 286
		ob_start();
		ob_implicit_flush(false);
		extract($_params_, EXTR_OVERWRITE);
		require($_file_);
		return ob_get_clean();
Qiang Xue committed
287 288
	}

Qiang Xue committed
289 290 291 292 293 294 295 296 297
	/**
	 * Renders dynamic content returned by the given PHP statements.
	 * This method is mainly used together with content caching (fragment caching and page caching)
	 * when some portions of the content (called *dynamic content*) should not be cached.
	 * The dynamic content must be returned by some PHP statements.
	 * @param string $statements the PHP statements for generating the dynamic content.
	 * @return string the placeholder of the dynamic content, or the dynamic content if there is no
	 * active content cache currently.
	 */
Qiang Xue committed
298 299
	public function renderDynamic($statements)
	{
Qiang Xue committed
300 301
		if (!empty($this->cacheStack)) {
			$n = count($this->dynamicPlaceholders);
Qiang Xue committed
302
			$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
Qiang Xue committed
303
			$this->addDynamicPlaceholder($placeholder, $statements);
Qiang Xue committed
304 305 306 307 308 309
			return $placeholder;
		} else {
			return $this->evaluateDynamicContent($statements);
		}
	}

Qiang Xue committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	/**
	 * Adds a placeholder for dynamic content.
	 * This method is internally used.
	 * @param string $placeholder the placeholder name
	 * @param string $statements the PHP statements for generating the dynamic content
	 */
	public function addDynamicPlaceholder($placeholder, $statements)
	{
		foreach ($this->cacheStack as $cache) {
			$cache->dynamicPlaceholders[$placeholder] = $statements;
		}
		$this->dynamicPlaceholders[$placeholder] = $statements;
	}

	/**
	 * Evaluates the given PHP statements.
	 * This method is mainly used internally to implement dynamic content feature.
	 * @param string $statements the PHP statements to be evaluated.
	 * @return mixed the return value of the PHP statements.
	 */
Qiang Xue committed
330 331 332 333 334
	public function evaluateDynamicContent($statements)
	{
		return eval($statements);
	}

Qiang Xue committed
335
	/**
Qiang Xue committed
336
	 * Begins recording a block.
337
	 * This method is a shortcut to beginning [[Block]]
Qiang Xue committed
338 339 340
	 * @param string $id the block ID.
	 * @param boolean $renderInPlace whether to render the block content in place.
	 * Defaults to false, meaning the captured block will not be displayed.
341
	 * @return Block the Block widget instance
Qiang Xue committed
342
	 */
Qiang Xue committed
343
	public function beginBlock($id, $renderInPlace = false)
Qiang Xue committed
344
	{
Alexander Makarov committed
345
		return Block::begin([
Qiang Xue committed
346 347
			'id' => $id,
			'renderInPlace' => $renderInPlace,
348
			'view' => $this,
Alexander Makarov committed
349
		]);
Qiang Xue committed
350 351 352
	}

	/**
Qiang Xue committed
353
	 * Ends recording a block.
Qiang Xue committed
354
	 */
Qiang Xue committed
355
	public function endBlock()
Qiang Xue committed
356
	{
357
		Block::end();
Qiang Xue committed
358 359
	}

Qiang Xue committed
360 361
	/**
	 * Begins the rendering of content that is to be decorated by the specified view.
Qiang Xue committed
362
	 * This method can be used to implement nested layout. For example, a layout can be embedded
Sergey Gonimar committed
363
	 * in another layout file specified as '@app/views/layouts/base.php' like the following:
Qiang Xue committed
364 365
	 *
	 * ~~~
Sergey Gonimar committed
366
	 * <?php $this->beginContent('@app/views/layouts/base.php'); ?>
Qiang Xue committed
367 368 369 370 371 372
	 * ...layout content here...
	 * <?php $this->endContent(); ?>
	 * ~~~
	 *
	 * @param string $viewFile 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.
resurtm committed
373
	 * @param array $params the variables (name => value) to be extracted and made available in the decorative view.
374 375
	 * @return ContentDecorator the ContentDecorator widget instance
	 * @see ContentDecorator
Qiang Xue committed
376
	 */
Alexander Makarov committed
377
	public function beginContent($viewFile, $params = [])
Qiang Xue committed
378
	{
Alexander Makarov committed
379
		return ContentDecorator::begin([
Qiang Xue committed
380
			'viewFile' => $viewFile,
Qiang Xue committed
381
			'params' => $params,
382
			'view' => $this,
Alexander Makarov committed
383
		]);
Qiang Xue committed
384 385 386 387 388 389 390
	}

	/**
	 * Ends the rendering of content.
	 */
	public function endContent()
	{
391
		ContentDecorator::end();
Qiang Xue committed
392 393
	}

394 395 396 397 398 399 400 401
	/**
	 * Begins fragment caching.
	 * This method will display cached content if it is available.
	 * If not, it will start caching and would expect an [[endCache()]]
	 * call to end the cache and save the content into cache.
	 * A typical usage of fragment caching is as follows,
	 *
	 * ~~~
resurtm committed
402
	 * if ($this->beginCache($id)) {
403 404 405 406 407 408
	 *     // ...generate content here
	 *     $this->endCache();
	 * }
	 * ~~~
	 *
	 * @param string $id a unique ID identifying the fragment to be cached.
409
	 * @param array $properties initial property values for [[FragmentCache]]
410 411 412
	 * @return boolean whether you should generate the content for caching.
	 * False if the cached version is available.
	 */
Alexander Makarov committed
413
	public function beginCache($id, $properties = [])
414 415
	{
		$properties['id'] = $id;
416
		$properties['view'] = $this;
slavcodev committed
417
		/** @var FragmentCache $cache */
418
		$cache = FragmentCache::begin($properties);
Qiang Xue committed
419
		if ($cache->getCachedContent() !== false) {
420 421 422 423 424 425 426 427 428 429 430 431
			$this->endCache();
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Ends fragment caching.
	 */
	public function endCache()
	{
432
		FragmentCache::end();
433
	}
434 435

	/**
Alexander Makarov committed
436
	 * Marks the beginning of a page.
437 438 439 440 441
	 */
	public function beginPage()
	{
		ob_start();
		ob_implicit_flush(false);
442 443

		$this->trigger(self::EVENT_BEGIN_PAGE);
444 445 446
	}

	/**
Alexander Makarov committed
447
	 * Marks the ending of a page.
448 449 450
	 */
	public function endPage()
	{
451
		$this->trigger(self::EVENT_END_PAGE);
Alexander Makarov committed
452
		ob_end_flush();
453
	}
Zander Baldwin committed
454
}