From 2ecf1d92842913246c9b02c3350e6ca6c642004d Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 22 Mar 2013 08:50:57 -0400 Subject: [PATCH] view sip --- framework/base/Controller.php | 4 +-- framework/base/View.php | 59 +++++++++++++++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/framework/base/Controller.php b/framework/base/Controller.php index 8840cca..3069ce3 100644 --- a/framework/base/Controller.php +++ b/framework/base/Controller.php @@ -414,12 +414,10 @@ class Controller extends Component */ protected function findLayoutFile() { - /** @var $module Module */ + $module = $this->module; if (is_string($this->layout)) { - $module = $this->module; $view = $this->layout; } elseif ($this->layout === null) { - $module = $this->module; while ($module !== null && $module->layout === null) { $module = $module->module; } diff --git a/framework/base/View.php b/framework/base/View.php index 75a911d..9911cd7 100644 --- a/framework/base/View.php +++ b/framework/base/View.php @@ -79,20 +79,10 @@ class View extends Component * @return string the rendering result * @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; - $this->context = $context; - - $content = $this->renderFile($viewFile, $params); - - if ($layoutFile !== false) { - $content = $this->renderFile($layoutFile, array('content' => $content)); - } - - $this->context = $oldContext; - - return $content; + $viewFile = $this->findViewFile($this->context, $view); + return $this->renderFile($viewFile, $params); } /** @@ -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. * This method will use [[Yii::createObject()]] to create the widget. * @param string $class the widget class name or path alias