Skip to content

Commit

Permalink
Fixes yiisoft#4040: Added $viewFile and $params to the `EVENT_BEF…
Browse files Browse the repository at this point in the history
…ORE_RENDER` and `EVENT_AFTER_RENDER` events for `View`.
  • Loading branch information
qiangxue committed Oct 3, 2014
1 parent b63bd41 commit eec716d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions docs/guide/runtime-url-handling.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
URL Management
==============
URL Parsing and Generation
==========================

> Note: This section is under development.
Expand Down
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 #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)
- 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: Added `yii\base\Application::loadedModules` (qiangxue)
- Chg #2037: Dropped the support for using `yii\base\Module` as concrete module classes (qiangxue)
Expand Down
3 changes: 3 additions & 0 deletions framework/UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Upgrade from Yii 2.0 RC

* `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.

* 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
Expand Down
24 changes: 17 additions & 7 deletions framework/base/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public function renderFile($viewFile, $params = [], $context = null)
$output = '';
$this->_viewFiles[] = $viewFile;

if ($this->beforeRender()) {
if ($this->beforeRender($viewFile, $params)) {
Yii::trace("Rendering view file: $viewFile", __METHOD__);
$ext = pathinfo($viewFile, PATHINFO_EXTENSION);
if (isset($this->renderers[$ext])) {
Expand All @@ -246,7 +246,7 @@ public function renderFile($viewFile, $params = [], $context = null)
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
$this->afterRender($output);
$this->afterRender($viewFile, $params, $output);
}

array_pop($this->_viewFiles);
Expand All @@ -267,11 +267,16 @@ public function getViewFile()
* This method is invoked right before [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_BEFORE_RENDER]] event.
* 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.
*/
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);

return $event->isValid;
Expand All @@ -281,14 +286,19 @@ public function beforeRender()
* This method is invoked right after [[renderFile()]] renders a view file.
* The default implementation will trigger the [[EVENT_AFTER_RENDER]] event.
* 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
* 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)) {
$event = new ViewEvent;
$event->output = $output;
$event = new ViewEvent([
'viewFile' => $viewFile,
'params' => $params,
'output' => $output,
]);
$this->trigger(self::EVENT_AFTER_RENDER, $event);
$output = $event->output;
}
Expand Down
8 changes: 8 additions & 0 deletions framework/base/ViewEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
*/
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()]].
* Event handlers may modify this property and the modified output will be
Expand Down

0 comments on commit eec716d

Please sign in to comment.