Commit ad4cf9e8 by Qiang Xue

Added beforeRender and afterRender to View.

parent a421f9f1
...@@ -22,7 +22,9 @@ class ActionEvent extends Event ...@@ -22,7 +22,9 @@ class ActionEvent extends Event
*/ */
public $action; public $action;
/** /**
* @var boolean whether to continue running the action. * @var boolean whether to continue running the action. Event handlers of
* [[Controller::EVENT_BEFORE_ACTION]] may set this property to decide whether
* to continue running the current action.
*/ */
public $isValid = true; public $isValid = true;
......
...@@ -22,6 +22,15 @@ use yii\helpers\FileHelper; ...@@ -22,6 +22,15 @@ use yii\helpers\FileHelper;
class View extends Component class View extends Component
{ {
/** /**
* @event Event an event that is triggered by [[renderFile()]] right before it renders a view file.
*/
const EVENT_BEFORE_RENDER = 'beforeRender';
/**
* @event Event an event that is triggered by [[renderFile()]] right after it renders a view file.
*/
const EVENT_AFTER_RENDER = 'afterRender';
/**
* @var object the object that owns this view. This can be a controller, a widget, or any other object. * @var object the object that owns this view. This can be a controller, a widget, or any other object.
*/ */
public $context; public $context;
...@@ -47,7 +56,7 @@ class View extends Component ...@@ -47,7 +56,7 @@ class View extends Component
public $clips; public $clips;
/** /**
* @var Widget[] the widgets that are currently being rendered (not ended). This property * @var Widget[] the widgets that are currently being rendered (not ended). This property
* is maintained by [[beginWidget()]] and [[endWidget()]] methods. Do not modify it directly. * is maintained by [[beginWidget()]] and [[endWidget()]] methods. Do not modify it.
*/ */
public $widgetStack = array(); public $widgetStack = array();
/** /**
...@@ -140,10 +149,14 @@ class View extends Component ...@@ -140,10 +149,14 @@ class View extends Component
$this->context = $context; $this->context = $context;
} }
if ($this->renderer !== null) { $output = '';
$output = $this->renderer->render($this, $viewFile, $params); if ($this->beforeRender($viewFile)) {
} else { if ($this->renderer !== null) {
$output = $this->renderPhpFile($viewFile, $params); $output = $this->renderer->render($this, $viewFile, $params);
} else {
$output = $this->renderPhpFile($viewFile, $params);
}
$this->afterRender($viewFile, $output);
} }
$this->context = $oldContext; $this->context = $oldContext;
...@@ -152,6 +165,38 @@ class View extends Component ...@@ -152,6 +165,38 @@ class View extends Component
} }
/** /**
* 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
* @return boolean whether to continue rendering the view file.
*/
public function beforeRender($viewFile)
{
$event = new ViewEvent($viewFile);
$this->trigger(self::EVENT_BEFORE_RENDER, $event);
return $event->isValid;
}
/**
* 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 to be rendered
* @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($viewFile, &$output)
{
if ($this->hasEventHandlers(self::EVENT_AFTER_RENDER)) {
$event = new ViewEvent($viewFile);
$event->output = $output;
$this->trigger(self::EVENT_AFTER_RENDER, $event);
$output = $event->output;
}
}
/**
* Renders a view file as a PHP script. * Renders a view file as a PHP script.
* *
* This method treats the view file as a PHP script and includes the file. * This method treats the view file as a PHP script and includes the file.
......
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\base;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class ViewEvent extends Event
{
/**
* @var string the rendering result of [[View::renderFile()]].
* Event handlers may modify this property and the modified output will be
* returned by [[View::renderFile()]]. This property is only used
* by [[View::EVENT_AFTER_RENDER]] event.
*/
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
* [[View::EVENT_BEFORE_RENDER]] may set this property to decide whether
* to continue rendering the current view file.
*/
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 = array())
{
$this->viewFile = $viewFile;
parent::__construct($config);
}
}
\ 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