Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
ad4cf9e8
Commit
ad4cf9e8
authored
Apr 05, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added beforeRender and afterRender to View.
parent
a421f9f1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
6 deletions
+98
-6
ActionEvent.php
framework/base/ActionEvent.php
+3
-1
View.php
framework/base/View.php
+50
-5
ViewEvent.php
framework/base/ViewEvent.php
+45
-0
No files found.
framework/base/ActionEvent.php
View file @
ad4cf9e8
...
...
@@ -22,7 +22,9 @@ class ActionEvent extends Event
*/
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
;
...
...
framework/base/View.php
View file @
ad4cf9e8
...
...
@@ -22,6 +22,15 @@ use yii\helpers\FileHelper;
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.
*/
public
$context
;
...
...
@@ -47,7 +56,7 @@ class View extends Component
public
$clips
;
/**
* @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
();
/**
...
...
@@ -140,10 +149,14 @@ class View extends Component
$this
->
context
=
$context
;
}
if
(
$this
->
renderer
!==
null
)
{
$output
=
$this
->
renderer
->
render
(
$this
,
$viewFile
,
$params
);
}
else
{
$output
=
$this
->
renderPhpFile
(
$viewFile
,
$params
);
$output
=
''
;
if
(
$this
->
beforeRender
(
$viewFile
))
{
if
(
$this
->
renderer
!==
null
)
{
$output
=
$this
->
renderer
->
render
(
$this
,
$viewFile
,
$params
);
}
else
{
$output
=
$this
->
renderPhpFile
(
$viewFile
,
$params
);
}
$this
->
afterRender
(
$viewFile
,
$output
);
}
$this
->
context
=
$oldContext
;
...
...
@@ -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.
*
* This method treats the view file as a PHP script and includes the file.
...
...
framework/base/ViewEvent.php
0 → 100644
View file @
ad4cf9e8
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment