View.php 14.9 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
	/**
	 * @var string the default view file extension. This will be appended to view file names if they don't have file extensions.
	 */
70
	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 175 176 177 178 179 180 181
		if (pathinfo($file, PATHINFO_EXTENSION) !== '') {
			return $file;
		}
		$path = $file . '.' . $this->defaultExtension;
		if ($this->defaultExtension !== 'php' && !is_file($path)) {
			$path = $file . '.php';
		}
		return $path;
Qiang Xue committed
182 183
	}

Qiang Xue committed
184 185
	/**
	 * Renders a view file.
Qiang Xue committed
186
	 *
Qiang Xue committed
187 188
	 * 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
189
	 *
Qiang Xue committed
190 191 192 193 194 195
	 * 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
196
	 * @param string $viewFile the view file. This can be either a file path or a path alias.
Qiang Xue committed
197
	 * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
Qiang Xue committed
198 199
	 * @param object $context the context that the view should use for rendering the view. If null,
	 * existing [[context]] will be used.
Qiang Xue committed
200
	 * @return string the rendering result
Qiang Xue committed
201
	 * @throws InvalidParamException if the view file does not exist
Qiang Xue committed
202
	 */
Alexander Makarov committed
203
	public function renderFile($viewFile, $params = [], $context = null)
Qiang Xue committed
204
	{
Qiang Xue committed
205
		$viewFile = Yii::getAlias($viewFile);
206 207 208
		if ($this->theme !== null) {
			$viewFile = $this->theme->applyTo($viewFile);
		}
Qiang Xue committed
209 210 211 212 213 214
		if (is_file($viewFile)) {
			$viewFile = FileHelper::localize($viewFile);
		} else {
			throw new InvalidParamException("The view file does not exist: $viewFile");
		}

Qiang Xue committed
215
		$oldContext = $this->context;
Qiang Xue committed
216 217 218
		if ($context !== null) {
			$this->context = $context;
		}
Qiang Xue committed
219

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

		$this->context = $oldContext;

		return $output;
Qiang Xue committed
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 267 268 269 270 271 272 273
	/**
	 * 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
274
	/**
Qiang Xue committed
275 276 277 278 279 280
	 * 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
281 282
	 * This method should mainly be called by view renderer or [[renderFile()]].
	 *
Qiang Xue committed
283 284 285
	 * @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
286
	 */
Alexander Makarov committed
287
	public function renderPhpFile($_file_, $_params_ = [])
Qiang Xue committed
288
	{
Qiang Xue committed
289 290 291 292 293
		ob_start();
		ob_implicit_flush(false);
		extract($_params_, EXTR_OVERWRITE);
		require($_file_);
		return ob_get_clean();
Qiang Xue committed
294 295
	}

Qiang Xue committed
296 297 298 299 300 301 302 303 304
	/**
	 * 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
305 306
	public function renderDynamic($statements)
	{
Qiang Xue committed
307 308
		if (!empty($this->cacheStack)) {
			$n = count($this->dynamicPlaceholders);
Qiang Xue committed
309
			$placeholder = "<![CDATA[YII-DYNAMIC-$n]]>";
Qiang Xue committed
310
			$this->addDynamicPlaceholder($placeholder, $statements);
Qiang Xue committed
311 312 313 314 315 316
			return $placeholder;
		} else {
			return $this->evaluateDynamicContent($statements);
		}
	}

Qiang Xue committed
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
	/**
	 * 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
337 338 339 340 341
	public function evaluateDynamicContent($statements)
	{
		return eval($statements);
	}

Qiang Xue committed
342
	/**
Qiang Xue committed
343
	 * Begins recording a block.
344
	 * This method is a shortcut to beginning [[Block]]
Qiang Xue committed
345 346 347
	 * @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.
348
	 * @return Block the Block widget instance
Qiang Xue committed
349
	 */
Qiang Xue committed
350
	public function beginBlock($id, $renderInPlace = false)
Qiang Xue committed
351
	{
Alexander Makarov committed
352
		return Block::begin([
Qiang Xue committed
353 354
			'id' => $id,
			'renderInPlace' => $renderInPlace,
355
			'view' => $this,
Alexander Makarov committed
356
		]);
Qiang Xue committed
357 358 359
	}

	/**
Qiang Xue committed
360
	 * Ends recording a block.
Qiang Xue committed
361
	 */
Qiang Xue committed
362
	public function endBlock()
Qiang Xue committed
363
	{
364
		Block::end();
Qiang Xue committed
365 366
	}

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

	/**
	 * Ends the rendering of content.
	 */
	public function endContent()
	{
398
		ContentDecorator::end();
Qiang Xue committed
399 400
	}

401 402 403 404 405 406 407 408
	/**
	 * 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
409
	 * if ($this->beginCache($id)) {
410 411 412 413 414 415
	 *     // ...generate content here
	 *     $this->endCache();
	 * }
	 * ~~~
	 *
	 * @param string $id a unique ID identifying the fragment to be cached.
416
	 * @param array $properties initial property values for [[FragmentCache]]
417 418 419
	 * @return boolean whether you should generate the content for caching.
	 * False if the cached version is available.
	 */
Alexander Makarov committed
420
	public function beginCache($id, $properties = [])
421 422
	{
		$properties['id'] = $id;
423
		$properties['view'] = $this;
slavcodev committed
424
		/** @var FragmentCache $cache */
425
		$cache = FragmentCache::begin($properties);
Qiang Xue committed
426
		if ($cache->getCachedContent() !== false) {
427 428 429 430 431 432 433 434 435 436 437 438
			$this->endCache();
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Ends fragment caching.
	 */
	public function endCache()
	{
439
		FragmentCache::end();
440
	}
441 442

	/**
Alexander Makarov committed
443
	 * Marks the beginning of a page.
444 445 446 447 448
	 */
	public function beginPage()
	{
		ob_start();
		ob_implicit_flush(false);
449 450

		$this->trigger(self::EVENT_BEGIN_PAGE);
451 452 453
	}

	/**
Alexander Makarov committed
454
	 * Marks the ending of a page.
455 456 457
	 */
	public function endPage()
	{
458
		$this->trigger(self::EVENT_END_PAGE);
Alexander Makarov committed
459
		ob_end_flush();
460
	}
Zander Baldwin committed
461
}