You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

367 lines
12 KiB

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
12 years ago
use Yii;
13 years ago
use yii\base\Application;
12 years ago
use yii\util\FileHelper;
13 years ago
/**
12 years ago
* View represents a view object in the MVC pattern.
12 years ago
*
12 years ago
* View provides a set of methods (e.g. [[render()]]) for rendering purpose.
12 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class View extends Component
{
13 years ago
/**
12 years ago
* @var object the object that owns this view. This can be a controller, a widget, or any other object.
*/
12 years ago
public $context;
13 years ago
/**
12 years ago
* @var mixed custom parameters that are shared among view templates.
13 years ago
*/
12 years ago
public $params;
13 years ago
/**
12 years ago
* @var ViewRenderer|array the view renderer object or the configuration array for
* creating the view renderer. If not set, view files will be treated as normal PHP files.
13 years ago
*/
12 years ago
public $renderer;
13 years ago
/**
12 years ago
* @var Theme|array the theme object or the configuration array for creating the theme.
* If not set, it means theming is not enabled.
*/
12 years ago
public $theme;
12 years ago
/**
* @var array a list of named output clips. You can call [[beginClip()]] and [[endClip()]]
* to capture small fragments of a view. They can be later accessed at somewhere else
* through this property.
*/
public $clips;
12 years ago
/**
13 years ago
* @var Widget[] the widgets that are currently not ended
*/
12 years ago
private $_widgetStack = array();
13 years ago
12 years ago
/**
12 years ago
* Initializes the view component.
12 years ago
*/
12 years ago
public function init()
13 years ago
{
12 years ago
parent::init();
if (is_array($this->renderer)) {
$this->renderer = Yii::createObject($this->renderer);
}
if (is_array($this->theme)) {
$this->theme = Yii::createObject($this->theme);
13 years ago
}
}
/**
12 years ago
* Renders a view.
12 years ago
*
12 years ago
* This method will call [[findViewFile()]] to convert the view name into the corresponding view
* file path, and it will then call [[renderFile()]] to render the view.
13 years ago
*
12 years ago
* @param string $view the view name. Please refer to [[findViewFile()]] on how to specify this parameter.
12 years ago
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
12 years ago
* @param object $context the context that the view should use for rendering the view. If null,
* existing [[context]] will be used.
* @return string the rendering result
12 years ago
* @throws InvalidParamException if the view cannot be resolved or the view file does not exist.
* @see renderFile
* @see findViewFile
13 years ago
*/
12 years ago
public function render($view, $params = array(), $context = null)
13 years ago
{
12 years ago
$viewFile = $this->findViewFile($context, $view);
return $this->renderFile($viewFile, $params, $context);
13 years ago
}
13 years ago
/**
* Renders a view file.
12 years ago
*
12 years ago
* If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long
* as it is available.
*
12 years ago
* 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.
*
12 years ago
* @param string $viewFile the view file. This can be either a file path or a path alias.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
12 years ago
* @param object $context the context that the view should use for rendering the view. If null,
* existing [[context]] will be used.
* @return string the rendering result
12 years ago
* @throws InvalidParamException if the view file does not exist
12 years ago
*/
12 years ago
public function renderFile($viewFile, $params = array(), $context = null)
12 years ago
{
12 years ago
$viewFile = Yii::getAlias($viewFile);
if (is_file($viewFile)) {
if ($this->theme !== null) {
$viewFile = $this->theme->applyTo($viewFile);
}
$viewFile = FileHelper::localize($viewFile);
} else {
throw new InvalidParamException("The view file does not exist: $viewFile");
}
12 years ago
$oldContext = $this->context;
if ($context !== null) {
$this->context = $context;
}
12 years ago
12 years ago
if ($this->renderer !== null) {
12 years ago
$output = $this->renderer->render($this, $viewFile, $params);
12 years ago
} else {
12 years ago
$output = $this->renderPhpFile($viewFile, $params);
12 years ago
}
12 years ago
$this->context = $oldContext;
return $output;
12 years ago
}
/**
* 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.
*
12 years ago
* This method should mainly be called by view renderer or [[renderFile()]].
*
* @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
12 years ago
*/
public function renderPhpFile($_file_, $_params_ = array())
12 years ago
{
ob_start();
ob_implicit_flush(false);
extract($_params_, EXTR_OVERWRITE);
require($_file_);
return ob_get_clean();
12 years ago
}
/**
12 years ago
* Finds the view file based on the given view name.
*
* A view name can be specified in one of the following formats:
*
* - 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 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 the currently
* active module.
12 years ago
* - relative path (e.g. "index"): the actual view file will be looked for under [[Controller::viewPath|viewPath]]
* of the context object, assuming the context is either a [[Controller]] or a [[Widget]].
12 years ago
*
* If the view name does not contain a file extension, it will use the default one `.php`.
*
12 years ago
* @param object $context the view context object
12 years ago
* @param string $view the view name or the path alias of the view file.
* @return string the view file path. Note that the file may not exist.
12 years ago
* @throws InvalidParamException if the view file is an invalid path alias or the context cannot be
* used to determine the actual view file corresponding to the specified view.
12 years ago
*/
protected function findViewFile($context, $view)
{
12 years ago
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/main"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '//', 2) === 0) {
12 years ago
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '/', 1) === 0) {
// e.g. "/site/index"
12 years ago
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif ($context instanceof Controller || $context instanceof Widget) {
/** @var $context Controller|Widget */
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
} else {
throw new InvalidParamException("Unable to resolve the view file for '$view'.");
12 years ago
}
12 years ago
return FileHelper::getExtension($file) === '' ? $file . '.php' : $file;
12 years ago
}
/**
12 years ago
* Creates a widget.
* This method will use [[Yii::createObject()]] to create the widget.
* @param string $class the widget class name or path alias
* @param array $properties the initial property values of the widget.
* @return Widget the newly created widget instance
*/
13 years ago
public function createWidget($class, $properties = array())
{
$properties['class'] = $class;
12 years ago
return Yii::createObject($properties, $this->context);
13 years ago
}
12 years ago
/**
* Creates and runs a widget.
* Compared with [[createWidget()]], this method does one more thing: it will
* run the widget after it is created.
* @param string $class the widget class name or path alias
* @param array $properties the initial property values of the widget.
* @param boolean $captureOutput whether to capture the output of the widget and return it as a string
* @return string|Widget if $captureOutput is true, the output of the widget will be returned;
* otherwise the widget object will be returned.
*/
13 years ago
public function widget($class, $properties = array(), $captureOutput = false)
13 years ago
{
13 years ago
if ($captureOutput) {
ob_start();
ob_implicit_flush(false);
$widget = $this->createWidget($class, $properties);
$widget->run();
return ob_get_clean();
} else {
$widget = $this->createWidget($class, $properties);
$widget->run();
return $widget;
}
13 years ago
}
13 years ago
/**
* Begins a widget.
12 years ago
* This method is similar to [[createWidget()]] except that it will expect a matching
* [[endWidget()]] call after this.
* @param string $class the widget class name or path alias
* @param array $properties the initial property values of the widget.
13 years ago
* @return Widget the widget instance
*/
13 years ago
public function beginWidget($class, $properties = array())
{
$widget = $this->createWidget($class, $properties);
12 years ago
$this->_widgetStack[] = $widget;
return $widget;
13 years ago
}
13 years ago
/**
* Ends a widget.
* Note that the rendering result of the widget is directly echoed out.
* If you want to capture the rendering result of a widget, you may use
* [[createWidget()]] and [[Widget::run()]].
* @return Widget the widget instance
12 years ago
* @throws InvalidCallException if [[beginWidget()]] and [[endWidget()]] calls are not properly nested
13 years ago
*/
public function endWidget()
13 years ago
{
12 years ago
$widget = array_pop($this->_widgetStack);
if ($widget instanceof Widget) {
13 years ago
$widget->run();
return $widget;
} else {
12 years ago
throw new InvalidCallException("Unmatched beginWidget() and endWidget() calls.");
13 years ago
}
}
12 years ago
/**
* Begins recording a clip.
* This method is a shortcut to beginning [[yii\widgets\Clip]]
* @param string $id the clip ID.
* @param boolean $renderInPlace whether to render the clip content in place.
* Defaults to false, meaning the captured clip will not be displayed.
* @return \yii\widgets\Clip the Clip widget instance
* @see \yii\widgets\Clip
12 years ago
*/
public function beginClip($id, $renderInPlace = false)
{
return $this->beginWidget('yii\widgets\Clip', array(
'id' => $id,
'renderInPlace' => $renderInPlace,
'view' => $this,
));
}
/**
* Ends recording a clip.
*/
public function endClip()
{
$this->endWidget();
}
/**
* Begins the rendering of content that is to be decorated by the specified view.
* @param string $view 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.
* @param array $params the variables (name=>value) to be extracted and made available in the decorative view.
* @return \yii\widgets\ContentDecorator the ContentDecorator widget instance
* @see \yii\widgets\ContentDecorator
*/
public function beginContent($view, $params = array())
{
return $this->beginWidget('yii\widgets\ContentDecorator', array(
'view' => $this,
'viewName' => $view,
'params' => $params,
));
}
/**
* Ends the rendering of content.
*/
public function endContent()
{
$this->endWidget();
}
12 years ago
//
// /**
// * 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,
// *
// * ~~~
// * if($this->beginCache($id)) {
// * // ...generate content here
// * $this->endCache();
// * }
// * ~~~
// *
// * @param string $id a unique ID identifying the fragment to be cached.
// * @param array $properties initial property values for [[yii\widgets\OutputCache]]
// * @return boolean whether we need to generate content for caching. False if cached version is available.
// * @see endCache
// */
// public function beginCache($id, $properties = array())
// {
// $properties['id'] = $id;
// $cache = $this->beginWidget('yii\widgets\OutputCache', $properties);
// if ($cache->getIsContentCached()) {
// $this->endCache();
// return false;
// } else {
// return true;
// }
// }
//
// /**
// * Ends fragment caching.
// * This is an alias to [[endWidget()]]
// * @see beginCache
// */
// public function endCache()
// {
// $this->endWidget();
// }
//
}