Commit a5ee5b84 by Qiang Xue

view WIP

parent 2ecf1d92
...@@ -304,9 +304,13 @@ class Controller extends Component ...@@ -304,9 +304,13 @@ class Controller extends Component
*/ */
public function render($view, $params = array()) public function render($view, $params = array())
{ {
$viewFile = $this->findViewFile($view); $output = Yii::$app->getView()->render($view, $params, $this);
$layoutFile = $this->findLayoutFile(); $layoutFile = $this->findLayoutFile();
return Yii::$app->getView()->render($this, $viewFile, $params, $layoutFile); if ($layoutFile !== false) {
return Yii::$app->getView()->renderFile($layoutFile, array('content' => $output), $this);
} else {
return $output;
}
} }
/** /**
...@@ -319,7 +323,7 @@ class Controller extends Component ...@@ -319,7 +323,7 @@ class Controller extends Component
*/ */
public function renderPartial($view, $params = array()) public function renderPartial($view, $params = array())
{ {
return $this->renderFile($this->findViewFile($view), $params); return Yii::$app->getView()->render($view, $params, $this);
} }
/** /**
...@@ -331,7 +335,7 @@ class Controller extends Component ...@@ -331,7 +335,7 @@ class Controller extends Component
*/ */
public function renderFile($file, $params = array()) public function renderFile($file, $params = array())
{ {
return Yii::$app->getView()->render($this, $file, $params); return Yii::$app->getView()->renderFile($file, $params, $this);
} }
/** /**
...@@ -346,46 +350,6 @@ class Controller extends Component ...@@ -346,46 +350,6 @@ class Controller 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($view)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/common"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '/', 1) !== 0) {
// e.g. "index"
$file = $this->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif (strncmp($view, '//', 2) !== 0) {
// e.g. "/site/index"
$file = $this->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} else {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
}
if (FileHelper::getExtension($file) === '') {
$file .= '.php';
}
return $file;
}
/**
* Finds the applicable layout file. * Finds the applicable layout file.
* *
* This method locates an applicable layout file via two steps. * This method locates an applicable layout file via two steps.
......
...@@ -80,8 +80,7 @@ class Widget extends Component ...@@ -80,8 +80,7 @@ class Widget extends Component
*/ */
public function render($view, $params = array()) public function render($view, $params = array())
{ {
$file = $this->findViewFile($view); return Yii::$app->getView()->render($view, $params, $this);
return Yii::$app->getView()->render($this, $file, $params);
} }
/** /**
...@@ -93,7 +92,7 @@ class Widget extends Component ...@@ -93,7 +92,7 @@ class Widget extends Component
*/ */
public function renderFile($file, $params = array()) public function renderFile($file, $params = array())
{ {
return Yii::$app->getView()->render($this, $file, $params); return Yii::$app->getView()->renderFile($file, $params, $this);
} }
/** /**
...@@ -107,44 +106,4 @@ class Widget extends Component ...@@ -107,44 +106,4 @@ class Widget extends Component
$class = new \ReflectionClass($className); $class = new \ReflectionClass($className);
return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views'; return dirname($class->getFileName()) . DIRECTORY_SEPARATOR . 'views';
} }
/**
* Finds the view file based on the given view name.
*
* The 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
*/
public function findViewFile($view)
{
if (strncmp($view, '@', 1) === 0) {
// e.g. "@app/views/common"
$file = Yii::getAlias($view);
} elseif (strncmp($view, '/', 1) !== 0) {
// e.g. "index"
$file = $this->getViewPath() . DIRECTORY_SEPARATOR . $view;
} elseif (strncmp($view, '//', 2) !== 0 && Yii::$app->controller !== null) {
// e.g. "/site/index"
$file = Yii::$app->controller->module->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
} else {
// e.g. "//layouts/main"
$file = Yii::$app->getViewPath() . DIRECTORY_SEPARATOR . ltrim($view, '/');
}
if (FileHelper::getExtension($file) === '') {
$file .= '.php';
}
return $file;
}
} }
\ No newline at end of file
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\widgets;
use Yii;
use yii\base\Widget;
use yii\base\View;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Clip extends Widget
{
/**
* @var string the ID of this clip.
*/
public $id;
/**
* @var View the view object for keeping the clip. If not set, the view registered with the application
* will be used.
*/
public $view;
/**
* @var boolean whether to render the clip content in place. Defaults to false,
* meaning the captured clip will not be displayed.
*/
public $renderInPlace = false;
/**
* Starts recording a clip.
*/
public function init()
{
ob_start();
ob_implicit_flush(false);
}
/**
* Ends recording a clip.
* This method stops output buffering and saves the rendering result as a named clip in the controller.
*/
public function run()
{
$clip = ob_get_clean();
if ($this->renderClip) {
echo $clip;
}
$view = $this->view !== null ? $this->view : Yii::$app->getView();
$view->clips[$this->id] = $clip;
}
}
\ No newline at end of file
<?php
/**
* CContentDecorator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright 2008-2013 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
/**
* CContentDecorator decorates the content it encloses with the specified view.
*
* CContentDecorator is mostly used to implement nested layouts, i.e., a layout
* is embedded within another layout. {@link CBaseController} defines a pair of
* convenient methods to use CContentDecorator:
* <pre>
* $this->beginContent('path/to/view');
* // ... content to be decorated
* $this->endContent();
* </pre>
*
* The property {@link view} specifies the name of the view that is used to
* decorate the content. In the view, the content being decorated may be
* accessed with variable <code>$content</code>.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @package system.web.widgets
* @since 1.0
*/
class CContentDecorator extends COutputProcessor
{
/**
* @var mixed the name of the view that will be used to decorate the captured content.
* If this property is null (default value), 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}.
*/
public $view;
/**
* @var array the variables (name=>value) to be extracted and made available in the decorative view.
*/
public $data=array();
/**
* Processes the captured output.
* This method decorates the output with the specified {@link view}.
* @param string $output the captured output to be processed
*/
public function processOutput($output)
{
$output=$this->decorate($output);
parent::processOutput($output);
}
/**
* Decorates the content by rendering a view and embedding the content in it.
* The content being embedded can be accessed in the view using variable <code>$content</code>
* The decorated content will be displayed directly.
* @param string $content the content to be decorated
* @return string the decorated content
*/
protected function decorate($content)
{
$owner=$this->getOwner();
if($this->view===null)
$viewFile=Yii::app()->getController()->getLayoutFile(null);
else
$viewFile=$owner->getViewFile($this->view);
if($viewFile!==false)
{
$data=$this->data;
$data['content']=$content;
return $owner->renderFile($viewFile,$data,true);
}
else
return $content;
}
}
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