Commit eec716d8 by Qiang Xue

Fixes #4040: Added `$viewFile` and `$params` to the `EVENT_BEFORE_RENDER` and…

Fixes #4040: Added `$viewFile` and `$params` to the `EVENT_BEFORE_RENDER` and `EVENT_AFTER_RENDER` events for `View`.
parent b63bd412
URL Management URL Parsing and Generation
============== ==========================
> Note: This section is under development. > Note: This section is under development.
......
...@@ -7,6 +7,7 @@ Yii Framework 2 Change Log ...@@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #5260: `yii\i18n\Formatter::decimalSeparator` and `yii\i18n\Formatter::thousandSeparator` where not configurable when intl is not installed (execut, cebe) - Bug #5260: `yii\i18n\Formatter::decimalSeparator` and `yii\i18n\Formatter::thousandSeparator` where not configurable when intl is not installed (execut, cebe)
- Bug #5314: Fixed typo in the implementation of `yii\web\Session::getHasSessionId()` (qiangxue) - Bug #5314: Fixed typo in the implementation of `yii\web\Session::getHasSessionId()` (qiangxue)
- Bug: Date and time formatting now assumes UTC as the timezone for input dates unless a timezone is explicitly given (cebe) - Bug: Date and time formatting now assumes UTC as the timezone for input dates unless a timezone is explicitly given (cebe)
- Enh #4040: Added `$viewFile` and `$params` to the `EVENT_BEFORE_RENDER` and `EVENT_AFTER_RENDER` events for `View` (qiangxue)
- Enh #4275: Added `removeChildren()` to `yii\rbac\ManagerInterface` and implementations (samdark) - Enh #4275: Added `removeChildren()` to `yii\rbac\ManagerInterface` and implementations (samdark)
- Enh: Added `yii\base\Application::loadedModules` (qiangxue) - Enh: Added `yii\base\Application::loadedModules` (qiangxue)
- Chg #2037: Dropped the support for using `yii\base\Module` as concrete module classes (qiangxue) - Chg #2037: Dropped the support for using `yii\base\Module` as concrete module classes (qiangxue)
......
...@@ -22,6 +22,9 @@ Upgrade from Yii 2.0 RC ...@@ -22,6 +22,9 @@ Upgrade from Yii 2.0 RC
* `yii\bootstrap\Collapse` is now encoding labels by default. `encode` item option and global `encodeLabels` property were * `yii\bootstrap\Collapse` is now encoding labels by default. `encode` item option and global `encodeLabels` property were
introduced to disable it. Keys are no longer used as labels. You need to remove keys and use `label` item option instead. introduced to disable it. Keys are no longer used as labels. You need to remove keys and use `label` item option instead.
* The `yii\base\View::beforeRender()` and `yii\base\View::afterRender()` methods have two extra parameters `$viewFile`
and `$params`. If you are overriding these methods, you should adjust the method signature accordingly.
Upgrade from Yii 2.0 Beta Upgrade from Yii 2.0 Beta
------------------------- -------------------------
......
...@@ -233,7 +233,7 @@ class View extends Component ...@@ -233,7 +233,7 @@ class View extends Component
$output = ''; $output = '';
$this->_viewFiles[] = $viewFile; $this->_viewFiles[] = $viewFile;
if ($this->beforeRender()) { if ($this->beforeRender($viewFile, $params)) {
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])) {
...@@ -246,7 +246,7 @@ class View extends Component ...@@ -246,7 +246,7 @@ class View extends Component
} else { } else {
$output = $this->renderPhpFile($viewFile, $params); $output = $this->renderPhpFile($viewFile, $params);
} }
$this->afterRender($output); $this->afterRender($viewFile, $params, $output);
} }
array_pop($this->_viewFiles); array_pop($this->_viewFiles);
...@@ -267,11 +267,16 @@ class View extends Component ...@@ -267,11 +267,16 @@ 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.
* @param array $params the parameter array passed to the [[render()]] method.
* @return boolean whether to continue rendering the view file. * @return boolean whether to continue rendering the view file.
*/ */
public function beforeRender() public function beforeRender($viewFile, $params)
{ {
$event = new ViewEvent; $event = new ViewEvent([
'viewFile' => $viewFile,
'params' => $params,
]);
$this->trigger(self::EVENT_BEFORE_RENDER, $event); $this->trigger(self::EVENT_BEFORE_RENDER, $event);
return $event->isValid; return $event->isValid;
...@@ -281,14 +286,19 @@ class View extends Component ...@@ -281,14 +286,19 @@ 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 being rendered.
* @param array $params the parameter array passed to the [[render()]] method.
* @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(&$output) public function afterRender($viewFile, $params, &$output)
{ {
if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) { if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
$event = new ViewEvent; $event = new ViewEvent([
$event->output = $output; 'viewFile' => $viewFile,
'params' => $params,
'output' => $output,
]);
$this->trigger(self::EVENT_AFTER_RENDER, $event); $this->trigger(self::EVENT_AFTER_RENDER, $event);
$output = $event->output; $output = $event->output;
} }
......
...@@ -16,6 +16,14 @@ namespace yii\base; ...@@ -16,6 +16,14 @@ namespace yii\base;
class ViewEvent extends Event class ViewEvent extends Event
{ {
/** /**
* @var string the view file being rendered.
*/
public $viewFile;
/**
* @var array the parameter array passed to the [[View::render()]] method.
*/
public $params;
/**
* @var string the rendering result of [[View::renderFile()]]. * @var string the rendering result of [[View::renderFile()]].
* Event handlers may modify this property and the modified output will be * Event handlers may modify this property and the modified output will be
* returned by [[View::renderFile()]]. This property is only used * returned by [[View::renderFile()]]. This property is only used
......
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