Commit 2ecf1d92 by Qiang Xue

view sip

parent 1d48d01e
...@@ -414,12 +414,10 @@ class Controller extends Component ...@@ -414,12 +414,10 @@ class Controller extends Component
*/ */
protected function findLayoutFile() protected function findLayoutFile()
{ {
/** @var $module Module */ $module = $this->module;
if (is_string($this->layout)) { if (is_string($this->layout)) {
$module = $this->module;
$view = $this->layout; $view = $this->layout;
} elseif ($this->layout === null) { } elseif ($this->layout === null) {
$module = $this->module;
while ($module !== null && $module->layout === null) { while ($module !== null && $module->layout === null) {
$module = $module->module; $module = $module->module;
} }
......
...@@ -79,20 +79,10 @@ class View extends Component ...@@ -79,20 +79,10 @@ class View extends Component
* @return string the rendering result * @return string the rendering result
* @throws InvalidParamException if the view file or the layout file does not exist. * @throws InvalidParamException if the view file or the layout file does not exist.
*/ */
public function render($context, $viewFile, $params = array(), $layoutFile = false) public function render($view, $params = array())
{ {
$oldContext = $this->context; $viewFile = $this->findViewFile($this->context, $view);
$this->context = $context; return $this->renderFile($viewFile, $params);
$content = $this->renderFile($viewFile, $params);
if ($layoutFile !== false) {
$content = $this->renderFile($layoutFile, array('content' => $content));
}
$this->context = $oldContext;
return $content;
} }
/** /**
...@@ -156,6 +146,49 @@ class View extends Component ...@@ -156,6 +146,49 @@ class View extends Component
} }
/** /**
* 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.
* - relative path (e.g. "index"): the actual view file will be looked for under [[viewPath]].
*
* If the view name does not contain a file extension, it will use the default one `.php`.
*
* @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.
* @throws InvalidParamException if the view file is an invalid path alias
*/
protected function findViewFile($context, $view)
{
if (FileHelper::getExtension($view) === '') {
$view .= '.php';
}
if (strncmp($view, '//', 2) === 0) {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '/', 1) === 0) {
// e.g. "/site/index"
$file = Yii::$app->controller->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} elseif (strncmp($view, '@', 1) !== 0) {
// e.g. "index" or "view/item"
if (method_exists($context, 'getViewPath')) {
$file = $context->getViewPath() . DIRECTORY_SEPARATOR . $view;
} else {
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . $view;
}
}
return $file;
}
/**
* Creates a widget. * Creates a widget.
* This method will use [[Yii::createObject()]] to create the widget. * This method will use [[Yii::createObject()]] to create the widget.
* @param string $class the widget class name or path alias * @param string $class the widget class name or path alias
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment