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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
02b908bd
Commit
02b908bd
authored
Jan 28, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #938: added `yii\web\View::renderAjax` and `yii\web\Controller::renderAjax`.
parent
861c2b2f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
3 deletions
+80
-3
Controller.php
framework/web/Controller.php
+20
-0
View.php
framework/web/View.php
+60
-3
No files found.
framework/web/Controller.php
View file @
02b908bd
...
...
@@ -16,6 +16,8 @@ use yii\helpers\Html;
*
* @property string $canonicalUrl The canonical URL of the currently requested page. This property is
* read-only.
* @property View $view The view object that can be used to render views or view files. This property is
* read-only.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
...
...
@@ -32,6 +34,24 @@ class Controller extends \yii\base\Controller
*/
public
$actionParams
=
[];
/**
* Renders a view in response to an AJAX request.
*
* This method is similar to [[renderPartial()]] except that it will inject into
* the rendering result with JS/CSS scripts and files which are registered with the view.
* For this reason, you should use this method instead of [[renderPartial()]] to render
* a view to respond to an AJAX request.
*
* @param string $view the view name. Please refer to [[render()]] on how to specify a view name.
* @param array $params the parameters (name-value pairs) that should be made available in the view.
* @return string the rendering result.
*/
public
function
renderAjax
(
$view
,
$params
=
[])
{
return
$this
->
getView
()
->
renderAjax
(
$view
,
$params
,
$this
);
}
/**
* Binds the parameters to the action.
* This method is invoked by [[Action]] when it begins to run with the given parameters.
...
...
framework/web/View.php
View file @
02b908bd
...
...
@@ -128,6 +128,38 @@ class View extends \yii\base\View
private
$_assetManager
;
/**
* Renders a view in response to an AJAX request.
*
* This method is similar to [[render()]] except that it will surround the view being rendered
* with the calls of [[beginPage()]], [[head()]], [[beginBody()]], [[endBody()]] and [[endPage()]].
* By doing so, the method is able to inject into the rendering result with JS/CSS scripts and files
* that are registered with the view.
*
* @param string $view the view name. Please refer to [[render()]] on how to specify this parameter.
* @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
* @param object $context the context that the view should use for rendering the view. If null,
* existing [[context]] will be used.
* @return string the rendering result
* @see render()
*/
public
function
renderAjax
(
$view
,
$params
=
[],
$context
=
null
)
{
$viewFile
=
$this
->
findViewFile
(
$view
,
$context
);
ob_start
();
ob_implicit_flush
(
false
);
$this
->
beginPage
();
$this
->
head
();
$this
->
beginBody
();
echo
$this
->
renderFile
(
$viewFile
,
$params
,
$context
);
$this
->
endBody
();
$this
->
endPage
(
true
);
return
ob_get_clean
();
}
/**
* Registers the asset manager being used by this view object.
* @return \yii\web\AssetManager the asset manager. Defaults to the "assetManager" application component.
*/
...
...
@@ -147,8 +179,11 @@ class View extends \yii\base\View
/**
* Marks the ending of an HTML page.
* @param boolean $ajaxMode whether the view is rendering in AJAX mode.
* If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
* will be rendered at the end of the view like normal scripts.
*/
public
function
endPage
()
public
function
endPage
(
$ajaxMode
=
false
)
{
$this
->
trigger
(
self
::
EVENT_END_PAGE
);
...
...
@@ -159,7 +194,7 @@ class View extends \yii\base\View
echo
strtr
(
$content
,
[
self
::
PH_HEAD
=>
$this
->
renderHeadHtml
(),
self
::
PH_BODY_BEGIN
=>
$this
->
renderBodyBeginHtml
(),
self
::
PH_BODY_END
=>
$this
->
renderBodyEndHtml
(),
self
::
PH_BODY_END
=>
$this
->
renderBodyEndHtml
(
$ajaxMode
),
]);
$this
->
metaTags
=
null
;
...
...
@@ -450,14 +485,34 @@ class View extends \yii\base\View
/**
* Renders the content to be inserted at the end of the body section.
* The content is rendered using the registered JS code blocks and files.
* @param boolean $ajaxMode whether the view is rendering in AJAX mode.
* If true, the JS scripts registered at [[POS_READY]] and [[POS_LOAD]] positions
* will be rendered at the end of the view like normal scripts.
* @return string the rendered content
*/
protected
function
renderBodyEndHtml
()
protected
function
renderBodyEndHtml
(
$ajaxMode
)
{
$lines
=
[];
if
(
!
empty
(
$this
->
jsFiles
[
self
::
POS_END
]))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
jsFiles
[
self
::
POS_END
]);
}
if
(
$ajaxMode
)
{
$scripts
=
[];
if
(
!
empty
(
$this
->
js
[
self
::
POS_END
]))
{
$scripts
[]
=
implode
(
"
\n
"
,
$this
->
js
[
self
::
POS_END
]);
}
if
(
!
empty
(
$this
->
js
[
self
::
POS_READY
]))
{
$scripts
[]
=
implode
(
"
\n
"
,
$this
->
js
[
self
::
POS_READY
]);
}
if
(
!
empty
(
$this
->
js
[
self
::
POS_LOAD
]))
{
$scripts
[]
=
implode
(
"
\n
"
,
$this
->
js
[
self
::
POS_LOAD
]);
}
if
(
!
empty
(
$scripts
))
{
$lines
[]
=
Html
::
script
(
implode
(
"
\n
"
,
$scripts
),
[
'type'
=>
'text/javascript'
]);
}
}
else
{
if
(
!
empty
(
$this
->
js
[
self
::
POS_END
]))
{
$lines
[]
=
Html
::
script
(
implode
(
"
\n
"
,
$this
->
js
[
self
::
POS_END
]),
[
'type'
=>
'text/javascript'
]);
}
...
...
@@ -469,6 +524,8 @@ class View extends \yii\base\View
$js
=
"jQuery(window).load(function(){\n"
.
implode
(
"
\n
"
,
$this
->
js
[
self
::
POS_LOAD
])
.
"
\n
});"
;
$lines
[]
=
Html
::
script
(
$js
,
[
'type'
=>
'text/javascript'
]);
}
}
return
empty
(
$lines
)
?
''
:
implode
(
"
\n
"
,
$lines
);
}
}
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