Yii2 Bootstrap 3
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.

494 lines
18 KiB

<?php
/**
* View class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
12 years ago
use Yii;
13 years ago
use yii\util\FileHelper;
13 years ago
use yii\base\Application;
13 years ago
/**
12 years ago
* View represents a view object in the MVC pattern.
*
* View provides a set of methods (e.g. [[render()]]) for rendering purpose.
*
* @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.
*/
public $owner;
/**
12 years ago
* @var string the layout to be applied when [[render()]] or [[renderContent()]] is called.
* If not set, it will use the [[Module::layout]] of the currently active module.
13 years ago
*/
12 years ago
public $layout;
13 years ago
/**
* @var string the language that the view should be rendered in. If not set, it will use
* the value of [[Application::language]].
*/
public $language;
/**
* @var string the language that the original view is in. If not set, it will use
* the value of [[Application::sourceLanguage]].
*/
public $sourceLanguage;
/**
13 years ago
* @var boolean whether to localize the view when possible. Defaults to true.
* Note that when this is true, if a localized view cannot be found, the original view will be rendered.
* No error will be reported.
*/
12 years ago
public $enableI18N = true;
13 years ago
/**
* @var boolean whether to theme the view when possible. Defaults to true.
12 years ago
* Note that theming will be disabled if [[Application::theme]] is not set.
13 years ago
*/
12 years ago
public $enableTheme = true;
13 years ago
/**
* @var mixed custom parameters that are available in the view template
*/
public $params;
12 years ago
/**
13 years ago
* @var Widget[] the widgets that are currently not ended
*/
12 years ago
private $_widgetStack = array();
13 years ago
/**
13 years ago
* Constructor.
12 years ago
* @param object $owner the owner of this view. This usually is a controller or a widget.
* @param array $config name-value pairs that will be used to initialize the object properties
13 years ago
*/
12 years ago
public function __construct($owner, $config = array())
13 years ago
{
12 years ago
$this->owner = $owner;
parent::__construct($config);
13 years ago
}
12 years ago
/**
12 years ago
* Renders a view within a layout.
* This method is similar to [[renderPartial()]] except that if a layout is available,
12 years ago
* this method will embed the view result into the layout and then return it.
12 years ago
* @param string $view the view to be rendered. Please refer to [[findViewFile()]] on possible formats of the view name.
12 years ago
* @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
12 years ago
* @throws InvalidConfigException if the view file or layout file cannot be found
* @see findViewFile()
* @see findLayoutFile()
12 years ago
*/
13 years ago
public function render($view, $params = array())
{
$content = $this->renderPartial($view, $params);
12 years ago
return $this->renderContent($content);
13 years ago
}
12 years ago
/**
12 years ago
* Renders a text content within a layout.
* The layout being used is resolved by [[findLayout()]].
* If no layout is available, the content will be returned back.
12 years ago
* @param string $content the content to be rendered
* @return string the rendering result
12 years ago
* @throws InvalidConfigException if the layout file cannot be found
* @see findLayoutFile()
12 years ago
*/
public function renderContent($content)
13 years ago
{
$layoutFile = $this->findLayoutFile();
if ($layoutFile !== false) {
12 years ago
return $this->renderFile($layoutFile, array('content' => $content));
13 years ago
} else {
12 years ago
return $content;
13 years ago
}
}
/**
13 years ago
* Renders a view.
*
13 years ago
* The method first finds the actual view file corresponding to the specified view.
13 years ago
* 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.
*
12 years ago
* @param string $view the view to be rendered. Please refer to [[findViewFile()]] on possible formats of the view name.
13 years ago
* @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
12 years ago
* @throws InvalidParamException if the view file cannot be found
12 years ago
* @see findViewFile()
13 years ago
*/
13 years ago
public function renderPartial($view, $params = array())
13 years ago
{
$file = $this->findViewFile($view);
if ($file !== false) {
13 years ago
return $this->renderFile($file, $params);
13 years ago
} else {
12 years ago
throw new InvalidParamException("Unable to find the view file for view '$view'.");
13 years ago
}
}
13 years ago
/**
* Renders a view file.
12 years ago
*
* If a [[ViewRenderer|view renderer]] is installed, this method will try to use the view renderer
* to render the view file. Otherwise, it will simply include the view file, capture its output
* and return it as a string.
*
* @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 renderFile($file, $params = array())
12 years ago
{
$renderer = Yii::$app->getViewRenderer();
if ($renderer !== null) {
return $renderer->render($this, $file, $params);
12 years ago
} else {
return $this->renderPhpFile($file, $params);
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.
*
* @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
* 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->owner);
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
* @throws Exception if [[beginWidget()]] and [[endWidget()]] calls are not properly nested
*/
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 {
throw new Exception("Unmatched beginWidget() and endWidget() calls.");
}
}
12 years ago
//
// /**
// * Begins recording a clip.
// * This method is a shortcut to beginning [[yii\widgets\Clip]]
// * @param string $id the clip ID.
// * @param array $properties initial property values for [[yii\widgets\Clip]]
// */
// public function beginClip($id, $properties = array())
// {
// $properties['id'] = $id;
// $this->beginWidget('yii\widgets\Clip', $properties);
// }
//
// /**
// * Ends recording a clip.
// */
// public function endClip()
// {
// $this->endWidget();
// }
//
// /**
// * 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();
// }
//
// /**
// * Begins the rendering of content that is to be decorated by the specified view.
// * @param mixed $view the name of the view that will be used to decorate the content. The actual view script
// * is resolved via {@link getViewFile}. If this parameter is null (default),
// * the default layout will be used as the decorative view.
// * Note that if the current controller does not belong to
// * any module, the default layout refers to the application's {@link CWebApplication::layout default layout};
// * If the controller belongs to a module, the default layout refers to the module's
// * {@link CWebModule::layout default layout}.
// * @param array $params the variables (name=>value) to be extracted and made available in the decorative view.
// * @see endContent
// * @see yii\widgets\ContentDecorator
// */
// public function beginContent($view, $params = array())
// {
// $this->beginWidget('yii\widgets\ContentDecorator', array(
// 'view' => $view,
// 'params' => $params,
// ));
// }
//
// /**
// * Ends the rendering of content.
// * @see beginContent
// */
// public function endContent()
// {
// $this->endWidget();
// }
13 years ago
13 years ago
/**
* Finds the view file based on the given view name.
12 years ago
*
12 years ago
* A view name can be specified in one of the following formats:
*
* - path alias (e.g. "@app/views/site/index");
12 years ago
* - 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.
* - relative path (e.g. "index"): the actual view file will be looked for under the [[owner]]'s view path.
* If [[owner]] is a widget or a controller, its view path is given by their `viewPath` property.
* If [[owner]] is an object of any other type, its view path is the `view` sub-directory of the directory
* containing the owner class file.
12 years ago
*
12 years ago
* If the view name does not contain a file extension, it will default to `.php`.
12 years ago
*
* If [[enableTheme]] is true and there is an active application them, the method will also
* attempt to use a themed version of the view file, when available.
*
12 years ago
* And if [[enableI18N]] is true, the method will attempt to use a translated version of the view file,
* when available.
*
13 years ago
* @param string $view the view name or path alias. If the view name does not specify
* the view file extension name, it will use `.php` as the extension name.
12 years ago
* @return string the view file path if it exists. False if the view file cannot be found.
* @throws InvalidConfigException if the view file does not exist
13 years ago
*/
13 years ago
public function findViewFile($view)
13 years ago
{
12 years ago
if (FileHelper::getExtension($view) === '') {
13 years ago
$view .= '.php';
13 years ago
}
13 years ago
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/common"
12 years ago
if (($file = Yii::getAlias($view)) === false) {
throw new InvalidConfigException("Invalid path alias: $view");
}
13 years ago
} elseif (strncmp($view, '/', 1) !== 0) {
12 years ago
// e.g. "index"
12 years ago
if ($this->owner instanceof Controller || $this->owner instanceof Widget) {
$file = $this->owner->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif ($this->owner !== null) {
$class = new \ReflectionClass($this->owner);
$file = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . $view;
12 years ago
} else {
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . $view;
13 years ago
}
} elseif (strncmp($view, '//', 2) !== 0 && Yii::$app->controller !== null) {
12 years ago
// e.g. "/site/index"
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
13 years ago
} else {
12 years ago
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
13 years ago
}
12 years ago
if (is_file($file)) {
if ($this->enableTheme && ($theme = Yii::$app->getTheme()) !== null) {
12 years ago
$file = $theme->apply($file);
}
return $this->enableI18N ? FileHelper::localize($file, $this->language, $this->sourceLanguage) : $file;
13 years ago
} else {
12 years ago
throw new InvalidConfigException("View file for view '$view' does not exist: $file");
13 years ago
}
}
/**
12 years ago
* Finds the layout file that can be applied to the view.
13 years ago
*
12 years ago
* The applicable layout is resolved according to the following rules:
13 years ago
*
12 years ago
* - If [[layout]] is specified as a string, use it as the layout name and search for the layout file
* under the layout path of the currently active module;
* - If [[layout]] is null and [[owner]] is a controller:
* * If the controller's [[Controller::layout|layout]] is a string, use it as the layout name
* and search for the layout file under the layout path of the parent module of the controller;
* * If the controller's [[Controller::layout|layout]] is null, look through its ancestor modules
12 years ago
* and find the first one whose [[Module::layout|layout]] is not null. Use the layout specified
* by that module;
12 years ago
* - Returns false for all other cases.
13 years ago
*
12 years ago
* Like view names, a layout name can take several formats:
*
* - path alias (e.g. "@app/views/layouts/main");
12 years ago
* - absolute path (e.g. "/main"): the layout name starts with a slash. The actual layout file will be
* looked for under the [[Application::layoutPath|layout path]] of the application;
* - relative path (e.g. "main"): the actual layout layout file will be looked for under the
* [[Module::viewPath|view path]] of the context module determined by the above layout resolution process.
*
* If the layout name does not contain a file extension, it will default to `.php`.
*
* If [[enableTheme]] is true and there is an active application them, the method will also
* attempt to use a themed version of the layout file, when available.
*
* And if [[enableI18N]] is true, the method will attempt to use a translated version of the layout file,
* when available.
*
* @return string|boolean the layout file path, or false if layout is not needed.
* @throws InvalidConfigException if the layout file cannot be found
13 years ago
*/
public function findLayoutFile()
{
12 years ago
/** @var $module Module */
12 years ago
if (is_string($this->layout)) {
if (Yii::$app->controller) {
$module = Yii::$app->controller->module;
12 years ago
} else {
$module = Yii::$app;
12 years ago
}
$view = $this->layout;
12 years ago
} elseif ($this->owner instanceof Controller) {
12 years ago
if (is_string($this->owner->layout)) {
$module = $this->owner->module;
$view = $this->owner->layout;
} elseif ($this->owner->layout === null) {
$module = $this->owner->module;
while ($module !== null && $module->layout === null) {
$module = $module->module;
}
12 years ago
if ($module !== null && is_string($module->layout)) {
$view = $module->layout;
12 years ago
}
}
12 years ago
}
if (!isset($view)) {
13 years ago
return false;
}
12 years ago
if (FileHelper::getExtension($view) === '') {
13 years ago
$view .= '.php';
}
if (strncmp($view, '@', 1) === 0) {
12 years ago
if (($file = Yii::getAlias($view)) === false) {
throw new InvalidConfigException("Invalid path alias: $view");
12 years ago
}
13 years ago
} elseif (strncmp($view, '/', 1) === 0) {
$file = Yii::$app->getLayoutPath() . DIRECTORY_SEPARATOR . $view;
13 years ago
} else {
12 years ago
$file = $module->getLayoutPath() . DIRECTORY_SEPARATOR . $view;
13 years ago
}
12 years ago
if (is_file($file)) {
if ($this->enableTheme && ($theme = Yii::$app->getTheme()) !== null) {
12 years ago
$file = $theme->apply($file);
}
return $this->enableI18N ? FileHelper::localize($file, $this->language, $this->sourceLanguage) : $file;
} else {
12 years ago
throw new InvalidConfigException("Layout file for layout '$view' does not exist: $file");
}
13 years ago
}
}