mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-16 14:29:30 +08:00
Added beforeRender and afterRender to View.
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,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.
|
||||||
*/
|
*/
|
||||||
@@ -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
|
|||||||
$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;
|
||||||
@@ -151,6 +164,38 @@ class View extends Component
|
|||||||
return $output;
|
return $output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
*
|
*
|
||||||
|
|||||||
44
framework/base/ViewEvent.php
Normal file
44
framework/base/ViewEvent.php
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user