Commit 0c85d56b by Qiang Xue

finished view.

parent 2746b6ed
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
use yii\base\Exception;
/** /**
* Gets the application start timestamp. * Gets the application start timestamp.
*/ */
...@@ -126,7 +128,7 @@ class YiiBase ...@@ -126,7 +128,7 @@ class YiiBase
* will be included only when the class is being used. This parameter is used only when * will be included only when the class is being used. This parameter is used only when
* the path alias refers to a class. * the path alias refers to a class.
* @return string the class name or the directory that this alias refers to * @return string the class name or the directory that this alias refers to
* @throws \yii\base\Exception if the path alias is invalid * @throws Exception if the path alias is invalid
*/ */
public static function import($alias, $forceInclude = false) public static function import($alias, $forceInclude = false)
{ {
...@@ -153,7 +155,7 @@ class YiiBase ...@@ -153,7 +155,7 @@ class YiiBase
} }
if (($path = static::getAlias(dirname($alias))) === false) { if (($path = static::getAlias(dirname($alias))) === false) {
throw new \yii\base\Exception('Invalid path alias: ' . $alias); throw new Exception('Invalid path alias: ' . $alias);
} }
if ($isClass) { if ($isClass) {
...@@ -184,10 +186,12 @@ class YiiBase ...@@ -184,10 +186,12 @@ class YiiBase
* *
* Note, this method does not ensure the existence of the resulting path. * Note, this method does not ensure the existence of the resulting path.
* @param string $alias alias * @param string $alias alias
* @return mixed path corresponding to the alias, false if the root alias is not previously registered. * @param boolean $throwException whether to throw exception if the alias is invalid.
* @return string|boolean path corresponding to the alias, false if the root alias is not previously registered.
* @throws Exception if the alias is invalid and $throwException is true.
* @see setAlias * @see setAlias
*/ */
public static function getAlias($alias) public static function getAlias($alias, $throwException = false)
{ {
if (isset(self::$aliases[$alias])) { if (isset(self::$aliases[$alias])) {
return self::$aliases[$alias]; return self::$aliases[$alias];
...@@ -199,7 +203,11 @@ class YiiBase ...@@ -199,7 +203,11 @@ class YiiBase
return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos); return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos);
} }
} }
return false; if ($throwException) {
throw new Exception("Invalid path alias: $alias");
} else {
return false;
}
} }
/** /**
...@@ -218,7 +226,7 @@ class YiiBase ...@@ -218,7 +226,7 @@ class YiiBase
* - a URL (e.g. `http://www.yiiframework.com`) * - a URL (e.g. `http://www.yiiframework.com`)
* - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the * - a path alias (e.g. `@yii/base`). In this case, the path alias will be converted into the
* actual path first by calling [[getAlias]]. * actual path first by calling [[getAlias]].
* @throws \yii\base\Exception if $path is an invalid alias * @throws Exception if $path is an invalid alias
* @see getAlias * @see getAlias
*/ */
public static function setAlias($alias, $path) public static function setAlias($alias, $path)
...@@ -230,7 +238,7 @@ class YiiBase ...@@ -230,7 +238,7 @@ class YiiBase
} elseif (($p = static::getAlias($path)) !== false) { } elseif (($p = static::getAlias($path)) !== false) {
self::$aliases[$alias] = $p; self::$aliases[$alias] = $p;
} else { } else {
throw new \yii\base\Exception('Invalid path: ' . $path); throw new Exception('Invalid path: ' . $path);
} }
} }
...@@ -291,7 +299,7 @@ class YiiBase ...@@ -291,7 +299,7 @@ class YiiBase
include($classFile); include($classFile);
return true; return true;
} else { } else {
throw new \yii\base\Exception("Class name '$className' does not match the class file '" . realpath($classFile) . "'. Have you checked their case sensitivity?"); throw new Exception("Class name '$className' does not match the class file '" . realpath($classFile) . "'. Have you checked their case sensitivity?");
} }
} }
...@@ -339,7 +347,7 @@ class YiiBase ...@@ -339,7 +347,7 @@ class YiiBase
* *
* @param string|array $config the configuration. It can be either a string or an array. * @param string|array $config the configuration. It can be either a string or an array.
* @return mixed the created object * @return mixed the created object
* @throws \yii\base\Exception if the configuration is invalid. * @throws Exception if the configuration is invalid.
* @see \yii\base\Object::newInstance() * @see \yii\base\Object::newInstance()
*/ */
public static function createObject($config) public static function createObject($config)
...@@ -351,7 +359,7 @@ class YiiBase ...@@ -351,7 +359,7 @@ class YiiBase
$class = $config['class']; $class = $config['class'];
unset($config['class']); unset($config['class']);
} else { } else {
throw new \yii\base\Exception('Object configuration must be an array containing a "class" element.'); throw new Exception('Object configuration must be an array containing a "class" element.');
} }
if (!class_exists($class, false)) { if (!class_exists($class, false)) {
......
...@@ -70,9 +70,12 @@ class Controller extends Component implements Initable ...@@ -70,9 +70,12 @@ class Controller extends Component implements Initable
*/ */
public $action; public $action;
/** /**
* @var View the view currently being used * @var string|boolean the name of the layout to be applied to this controller's views.
* This property mainly affects the behavior of [[render()]].
* Defaults to null, meaning the layout specified by the [[module]] should be used.
* If false, no layout will be applied.
*/ */
private $_view; public $layout;
/** /**
* @param string $id ID of this controller * @param string $id ID of this controller
...@@ -295,66 +298,39 @@ class Controller extends Component implements Initable ...@@ -295,66 +298,39 @@ class Controller extends Component implements Initable
/** /**
* This method is invoked right after an action renders its result using [[render()]]. * This method is invoked right after an action renders its result using [[render()]].
* @param string $view the view just rendered * @param string $view the view just rendered
* @param string $content the content to be displayed
* @return string the content to be displayed. This may be the same as the input content or the one
* modified by event handlers.
*/ */
public function afterRender($view) public function afterRender($view, $content)
{ {
$this->trigger(__METHOD__, new RenderEvent($view)); $event = new RenderEvent($view, $content);
$this->trigger(__METHOD__, $event);
return $event->content;
} }
public function render($view, $params = array()) public function render($view, $params = array())
{ {
if ($this->beforeRender($view)) { if ($this->beforeRender($view)) {
$v = $this->createView(); $content = $this->createView()->render($view, $params);
if (($theme = \Yii::$application->getTheme()) !== null) { $content = $this->afterRender($view, $content);
$v->basePath[] = $theme->getViewPath($this); return $content;
$v->rootPath[] = $theme->getViewPath();
}
$v->basePath[] = $this->getViewPath();
$v->rootPath[] = \Yii::$application->getViewPath();
$v->render($view, $params);
$this->afterRender($view);
} }
return '';
} }
public function renderText($text) public function renderText($text)
{ {
return $this->createView()->renderText($text);
} }
public function renderPartial($view, $params = array()) public function renderPartial($view, $params = array())
{ {
return $this->createView()->renderPartial($view, $params);
}
public function resolveLayout()
{
$layout = $this->layout;
$module = $this->module;
while ($layout === null && $module !== null) {
$layout = $module->layout;
$module = $module->module;
$layout = $
}
}
public function getView()
{
if ($this->_view === null) {
$this->_view = $this->createView();
$this->_view->owner = $this;
if (($theme = \Yii::$application->getTheme()) !== null) {
$this->_view->basePath[] = $theme->getViewPath($this);
$this->_view->rootPath[] = $theme->getViewPath();
}
$this->_view->basePath[] = $this->getViewPath();
$this->_view->rootPath[] = \Yii::$application->getViewPath();
}
return $this->_view;
} }
public function createView() public function createView()
{ {
return new View; return new View($this);
} }
} }
...@@ -319,7 +319,7 @@ class ErrorHandler extends ApplicationComponent ...@@ -319,7 +319,7 @@ class ErrorHandler extends ApplicationComponent
public function renderAsHtml($exception) public function renderAsHtml($exception)
{ {
$view = new View; $view = new View;
$view->owner = $this; $view->context = $this;
$name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView; $name = !YII_DEBUG || $exception instanceof HttpException ? $this->errorView : $this->exceptionView;
echo $view->render($name, array( echo $view->render($name, array(
'exception' => $exception, 'exception' => $exception,
......
...@@ -24,6 +24,10 @@ class RenderEvent extends Event ...@@ -24,6 +24,10 @@ class RenderEvent extends Event
*/ */
public $view; public $view;
/** /**
* @var string the content to be displayed
*/
public $content;
/**
* @var boolean whether the action is in valid state and its life cycle should proceed. * @var boolean whether the action is in valid state and its life cycle should proceed.
*/ */
public $isValid = true; public $isValid = true;
......
...@@ -23,90 +23,39 @@ class Theme extends ApplicationComponent ...@@ -23,90 +23,39 @@ class Theme extends ApplicationComponent
public function init() public function init()
{ {
if ($this->basePath !== null) { if ($this->basePath !== null) {
$this->basePath = \Yii::getAlias($this->basePath); $this->basePath = \Yii::getAlias($this->basePath, true);
} else { } else {
throw new Exception("Theme.basePath must be set."); throw new Exception("Theme.basePath must be set.");
} }
if ($this->baseUrl !== null) { if ($this->baseUrl !== null) {
$this->baseUrl = \Yii::getAlias($this->baseUrl); $this->baseUrl = \Yii::getAlias($this->baseUrl, true);
} else { } else {
throw new Exception("Theme.baseUrl must be set."); throw new Exception("Theme.baseUrl must be set.");
} }
} }
/** /**
* @param Controller $controller * @param Application|Module|Controller|Object $context
* @return string * @return string
*/ */
public function getViewPath($controller = null) public function getViewPath($context = null)
{ {
$path = $this->basePath . DIRECTORY_SEPARATOR . 'views'; $viewPath = $this->basePath . DIRECTORY_SEPARATOR . 'views';
return $controller === null ? $path : $path . DIRECTORY_SEPARATOR . $controller->id; if ($context === null || $context instanceof Application) {
} return $viewPath;
} elseif ($context instanceof Controller || $context instanceof Module) {
public function getLayoutPath($module = null) return $viewPath . DIRECTORY_SEPARATOR . $context->getUniqueId();
{ } else {
$path = $this->getViewPath($module); return $viewPath . DIRECTORY_SEPARATOR . str_replace('\\', '_', get_class($context));
return $controller === null ? $path : $path . DIRECTORY_SEPARATOR . $controller->id; }
}
public function getWidgetViewPath($widget)
{
}
/**
* @return string the path for controller views. Defaults to 'ThemeRoot/views'.
*/
public function getViewPath()
{
return $this->_basePath . DIRECTORY_SEPARATOR . 'views';
}
/**
* Finds the view file for the specified controller's view.
* @param CController $controller the controller
* @param string $viewName the view name
* @return string the view file path. False if the file does not exist.
*/
public function getViewFile($controller, $viewName)
{
$moduleViewPath = $this->getViewPath();
if (($module = $controller->getModule()) !== null)
{
$moduleViewPath .= '/' . $module->getId();
}
return $controller->resolveViewFile($viewName, $this->getViewPath() . '/' . $controller->getUniqueId(), $this->getViewPath(), $moduleViewPath);
} }
/** /**
* Finds the layout file for the specified controller's layout. * @param Module $module
* @param CController $controller the controller * @return string
* @param string $layoutName the layout name
* @return string the layout file path. False if the file does not exist.
*/ */
public function getLayoutFile($controller, $layoutName) public function getLayoutPath($module = null)
{ {
$moduleViewPath = $basePath = $this->getViewPath(); return $this->getViewPath($module) . DIRECTORY_SEPARATOR . 'layouts';
$module = $controller->getModule();
if (empty($layoutName)) {
while ($module !== null) {
if ($module->layout === false)
return false;
if (!empty($module->layout))
break;
$module = $module->getParentModule();
}
if ($module === null)
$layoutName = Yii::app()->layout;
else {
$layoutName = $module->layout;
$moduleViewPath .= '/' . $module->getId();
}
}
else if ($module !== null)
$moduleViewPath .= '/' . $module->getId();
return $controller->resolveViewFile($layoutName, $moduleViewPath . '/layouts', $basePath, $moduleViewPath);
} }
} }
...@@ -98,7 +98,7 @@ class Widget extends Component implements Initable ...@@ -98,7 +98,7 @@ class Widget extends Component implements Initable
*/ */
public function render($view, $params = array()) public function render($view, $params = array())
{ {
return $this->createView()->render($view, $params); return $this->createView()->renderPartial($view, $params);
} }
/** /**
...@@ -106,12 +106,6 @@ class Widget extends Component implements Initable ...@@ -106,12 +106,6 @@ class Widget extends Component implements Initable
*/ */
public function createView() public function createView()
{ {
$view = new View; return new View($this);
if (($theme = \Yii::$application->getTheme()) !== null) {
$view->basePath[] = $theme->getViewPath() . DIRECTORY_SEPARATOR . str_replace('\\', '_', get_class($this));
}
$class = new \ReflectionClass($this);
$view->basePath[] = dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
return $view;
} }
} }
\ No newline at end of file
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