Added View::viewFile and removed ViewEvent::viewFile

This commit is contained in:
Qiang Xue
2014-02-21 16:31:59 -05:00
parent d8d44b6127
commit 97d291ca6a
3 changed files with 16 additions and 23 deletions

View File

@ -195,6 +195,7 @@ Yii Framework 2 Change Log
- Renamed `yii\web\User::idVar` to `idParam` - Renamed `yii\web\User::idVar` to `idParam`
- Renamed `yii\web\User::authTimeoutVar` to `authTimeoutParam` - Renamed `yii\web\User::authTimeoutVar` to `authTimeoutParam`
- Renamed `yii\web\User::returnUrlVar` to `returnUrlParam` - Renamed `yii\web\User::returnUrlVar` to `returnUrlParam`
- Chg: Added `View::viewFile` and removed `ViewEvent::viewFile` (qiangxue)
- New #66: [Auth client library](https://github.com/yiisoft/yii2-authclient) OpenId, OAuth1, OAuth2 clients (klimov-paul) - New #66: [Auth client library](https://github.com/yiisoft/yii2-authclient) OpenId, OAuth1, OAuth2 clients (klimov-paul)
- New #706: Added `yii\widgets\Pjax` and enhanced `GridView` to work with `Pjax` to support AJAX-update (qiangxue) - New #706: Added `yii\widgets\Pjax` and enhanced `GridView` to work with `Pjax` to support AJAX-update (qiangxue)

View File

@ -92,6 +92,13 @@ class View extends Component
* @internal * @internal
*/ */
public $dynamicPlaceholders = []; public $dynamicPlaceholders = [];
/**
* @var string the path of the view file currently being rendered. If the view is not
* in the process of rendering a view, this property is null.
* This property is mainly provided for information purpose and is maintained by [[renderFile()]].
* Do not modify it.
*/
public $viewFile;
/** /**
@ -218,7 +225,8 @@ class View extends Component
} }
$output = ''; $output = '';
if ($this->beforeRender($viewFile)) { $this->viewFile = $viewFile;
if ($this->beforeRender()) {
Yii::trace("Rendering view file: $viewFile", __METHOD__); Yii::trace("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION); $ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) { if (isset($this->renderers[$ext])) {
@ -231,8 +239,9 @@ class View extends Component
} else { } else {
$output = $this->renderPhpFile($viewFile, $params); $output = $this->renderPhpFile($viewFile, $params);
} }
$this->afterRender($viewFile, $output); $this->afterRender($output);
} }
$this->viewFile = null;
$this->context = $oldContext; $this->context = $oldContext;
@ -243,12 +252,11 @@ class View extends Component
* This method is invoked right before [[renderFile()]] renders a view file. * This method is invoked right before [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event. * The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
* If you override this method, make sure you call the parent implementation first. * If you override this method, make sure you call the parent implementation first.
* @param string $viewFile the view file to be rendered
* @return boolean whether to continue rendering the view file. * @return boolean whether to continue rendering the view file.
*/ */
public function beforeRender($viewFile) public function beforeRender()
{ {
$event = new ViewEvent($viewFile); $event = new ViewEvent;
$this->trigger(self::EVENT_BEFORE_RENDER, $event); $this->trigger(self::EVENT_BEFORE_RENDER, $event);
return $event->isValid; return $event->isValid;
} }
@ -257,14 +265,13 @@ class View extends Component
* This method is invoked right after [[renderFile()]] renders a view file. * This method is invoked right after [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_AFTER_RENDER]] event. * The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
* If you override this method, make sure you call the parent implementation first. * If you override this method, make sure you call the parent implementation first.
* @param string $viewFile the view file to be rendered
* @param string $output the rendering result of the view file. Updates to this parameter * @param string $output the rendering result of the view file. Updates to this parameter
* will be passed back and returned by [[renderFile()]]. * will be passed back and returned by [[renderFile()]].
*/ */
public function afterRender($viewFile, &$output) public function afterRender(&$output)
{ {
if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) { if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
$event = new ViewEvent($viewFile); $event = new ViewEvent;
$event->output = $output; $event->output = $output;
$this->trigger(self::EVENT_AFTER_RENDER, $event); $this->trigger(self::EVENT_AFTER_RENDER, $event);
$output = $event->output; $output = $event->output;

View File

@ -22,25 +22,10 @@ class ViewEvent extends Event
* by [[View::EVENT_AFTER_RENDER]] event. * by [[View::EVENT_AFTER_RENDER]] event.
*/ */
public $output; public $output;
/**
* @var string the view file path that is being rendered by [[View::renderFile()]].
*/
public $viewFile;
/** /**
* @var boolean whether to continue rendering the view file. Event handlers of * @var boolean whether to continue rendering the view file. Event handlers of
* [[View::EVENT_BEFORE_RENDER]] may set this property to decide whether * [[View::EVENT_BEFORE_RENDER]] may set this property to decide whether
* to continue rendering the current view file. * to continue rendering the current view file.
*/ */
public $isValid = true; public $isValid = true;
/**
* Constructor.
* @param string $viewFile the view file path that is being rendered by [[View::renderFile()]].
* @param array $config name-value pairs that will be used to initialize the object properties
*/
public function __construct($viewFile, $config = [])
{
$this->viewFile = $viewFile;
parent::__construct($config);
}
} }