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
3a146a06
Commit
3a146a06
authored
Apr 27, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored view renderers.
parent
385f711f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
112 deletions
+58
-112
View.php
framework/base/View.php
+26
-8
CompositeViewRenderer.php
framework/renderers/CompositeViewRenderer.php
+0
-57
SmartyViewRenderer.php
framework/renderers/SmartyViewRenderer.php
+20
-25
TwigViewRenderer.php
framework/renderers/TwigViewRenderer.php
+12
-22
No files found.
framework/base/View.php
View file @
3a146a06
...
...
@@ -70,10 +70,25 @@ class View extends Component
*/
public
$params
;
/**
* @var ViewRenderer|array the view renderer object or the configuration array for
* creating the view renderer. If not set, view files will be treated as normal PHP files.
* @var array a list of available renderers indexed by their corresponding supported file extensions.
* Each renderer may be a view renderer object or the configuration for creating the renderer object.
* For example,
*
* ~~~
* array(
* 'tpl' => array(
* 'class' => 'yii\renderers\SmartyRenderer',
* ),
* 'twig' => array(
* 'class' => 'yii\renderers\TwigRenderer',
* ),
* )
* ~~~
*
* If no renderer is available for the given view file, the view file will be treated as a normal PHP
* and rendered via [[renderPhpFile()]].
*/
public
$renderer
;
public
$renderer
s
=
array
()
;
/**
* @var Theme|array the theme object or the configuration array for creating the theme object.
* If not set, it means theming is not enabled.
...
...
@@ -152,9 +167,6 @@ class View extends Component
public
function
init
()
{
parent
::
init
();
if
(
is_array
(
$this
->
renderer
))
{
$this
->
renderer
=
Yii
::
createObject
(
$this
->
renderer
);
}
if
(
is_array
(
$this
->
theme
))
{
$this
->
theme
=
Yii
::
createObject
(
$this
->
theme
);
}
...
...
@@ -226,8 +238,14 @@ class View extends Component
$output
=
''
;
if
(
$this
->
beforeRender
(
$viewFile
))
{
if
(
$this
->
renderer
!==
null
)
{
$output
=
$this
->
renderer
->
render
(
$this
,
$viewFile
,
$params
);
$ext
=
pathinfo
(
$viewFile
,
PATHINFO_EXTENSION
);
if
(
isset
(
$this
->
renderers
[
$ext
]))
{
if
(
is_array
(
$this
->
renderers
[
$ext
]))
{
$this
->
renderers
[
$ext
]
=
Yii
::
createObject
(
$this
->
renderers
[
$ext
]);
}
/** @var ViewRenderer $renderer */
$renderer
=
$this
->
renderers
[
$ext
];
$output
=
$renderer
->
render
(
$this
,
$viewFile
,
$params
);
}
else
{
$output
=
$this
->
renderPhpFile
(
$viewFile
,
$params
);
}
...
...
framework/renderers/CompositeViewRenderer.php
deleted
100644 → 0
View file @
385f711f
<?php
/**
* Composite view renderer class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright © 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\renderers
;
use
Yii
;
use
yii\base\View
;
use
yii\base\ViewRenderer
;
/**
* CompositeViewRenderer allows you to use multiple view renderers in a single
* application.
*
* @author Alexander Makarov <sam@rmcreative.ru>
* @since 2.0
*/
class
CompositeViewRenderer
extends
ViewRenderer
{
/**
* @var array a config array with the view renderer objects or the configuration arrays for
* creating the view renderers indexed by file extensions.
*/
public
$renderers
=
array
();
/**
* Renders a view file.
*
* This method is invoked by [[View]] whenever it tries to render a view.
* Child classes must implement this method to render the given view file.
*
* @param View $view the view object used for rendering the file.
* @param string $file the view file.
* @param array $params the parameters to be passed to the view file.
*
* @return string the rendering result
*/
public
function
render
(
$view
,
$file
,
$params
)
{
$ext
=
pathinfo
(
$file
,
PATHINFO_EXTENSION
);
if
(
$ext
===
'php'
||
!
isset
(
$this
->
renderers
[
$ext
]))
{
return
$view
->
renderPhpFile
(
$file
,
$params
);
}
else
{
if
(
is_array
(
$this
->
renderers
[
$ext
]))
{
$this
->
renderers
[
$ext
]
=
Yii
::
createObject
(
$this
->
renderers
[
$ext
]);
}
return
$this
->
renderers
[
$ext
]
->
render
(
$view
,
$file
,
$params
);
}
}
}
\ No newline at end of file
framework/renderers/SmartyViewRenderer.php
View file @
3a146a06
...
...
@@ -9,8 +9,10 @@
namespace
yii\renderers
;
use
\yii\base\View
;
use
\yii\base\ViewRenderer
;
use
Yii
;
use
Smarty
;
use
yii\base\View
;
use
yii\base\ViewRenderer
;
/**
* SmartyViewRenderer allows you to use Smarty templates in views.
...
...
@@ -21,34 +23,31 @@ use \yii\base\ViewRenderer;
class
SmartyViewRenderer
extends
ViewRenderer
{
/**
* @var string alias pointing to where Smarty code is located.
* @var string
the directory or path
alias pointing to where Smarty code is located.
*/
public
$smarty
Dir
=
'@app/vendors/Smarty'
;
public
$smarty
Path
=
'@app/vendors/Smarty'
;
/**
* @var string alias pointing to where Smarty cache will be stored.
* @var string
the directory or path
alias pointing to where Smarty cache will be stored.
*/
public
$cache
Dir
=
'@app/runtime/Smarty/cache'
;
public
$cache
Path
=
'@app/runtime/Smarty/cache'
;
/**
* @var string
alias pointing to where Smarty compiled tea
mplates will be stored.
* @var string
the directory or path alias pointing to where Smarty compiled te
mplates will be stored.
*/
public
$compile
Dir
=
'@app/runtime/Smarty/compile'
;
public
$compile
Path
=
'@app/runtime/Smarty/compile'
;
/**
* @var
string file extension to use for template files
* @var
Smarty
*/
public
$fileExtension
=
'tpl'
;
/** @var \Smarty */
protected
$_smarty
;
public
$smarty
;
public
function
init
()
{
require_once
(
\Yii
::
getAlias
(
$this
->
smartyDir
)
.
'/Smarty.class.php'
);
$this
->
_smarty
=
new
\
Smarty
();
$this
->
_smarty
->
setCompileDir
(
\Yii
::
getAlias
(
$this
->
compileDir
));
$this
->
_smarty
->
setCacheDir
(
\Yii
::
getAlias
(
$this
->
cacheDir
));
require_once
(
Yii
::
getAlias
(
$this
->
smartyPath
)
.
'/Smarty.class.php'
);
$this
->
smarty
=
new
Smarty
();
$this
->
smarty
->
setCompileDir
(
Yii
::
getAlias
(
$this
->
compilePath
));
$this
->
smarty
->
setCacheDir
(
Yii
::
getAlias
(
$this
->
cachePath
));
}
/**
...
...
@@ -66,13 +65,8 @@ class SmartyViewRenderer extends ViewRenderer
public
function
render
(
$view
,
$file
,
$params
)
{
$ext
=
pathinfo
(
$file
,
PATHINFO_EXTENSION
);
if
(
$ext
===
$this
->
fileExtension
)
{
/** @var \Smarty_Internal_Template $template */
$template
=
$this
->
_smarty
->
createTemplate
(
$file
,
null
,
null
,
$params
,
true
);
return
$template
->
fetch
();
}
else
{
return
$view
->
renderPhpFile
(
$file
,
$params
);
}
/** @var \Smarty_Internal_Template $template */
$template
=
$this
->
smarty
->
createTemplate
(
$file
,
null
,
null
,
$params
,
true
);
return
$template
->
fetch
();
}
}
\ No newline at end of file
framework/renderers/TwigViewRenderer.php
View file @
3a146a06
...
...
@@ -9,8 +9,9 @@
namespace
yii\renderers
;
use
\yii\base\View
;
use
\yii\base\ViewRenderer
;
use
Yii
;
use
yii\base\View
;
use
yii\base\ViewRenderer
;
/**
* TwigViewRenderer allows you to use Twig templates in views.
...
...
@@ -21,19 +22,14 @@ use \yii\base\ViewRenderer;
class
TwigViewRenderer
extends
ViewRenderer
{
/**
* @var string alias pointing to where Twig code is located.
* @var string
the directory or path
alias pointing to where Twig code is located.
*/
public
$twig
Dir
=
'@app/vendors/Twig'
;
public
$twig
Path
=
'@app/vendors/Twig'
;
/**
* @var string alias pointing to where Twig cache will be stored.
* @var string
the directory or path
alias pointing to where Twig cache will be stored.
*/
public
$cacheDir
=
'@app/runtime/Twig/cache'
;
/**
* @var string file extension to use for template files.
*/
public
$fileExtension
=
'twig'
;
public
$cachePath
=
'@app/runtime/Twig/cache'
;
/**
* @var array Twig options
...
...
@@ -44,16 +40,16 @@ class TwigViewRenderer extends ViewRenderer
/**
* @var \Twig_Environment
*/
p
rotected
$_
twig
;
p
ublic
$
twig
;
public
function
init
()
{
\Yii
::
setAlias
(
'@Twig'
,
$this
->
twigDir
);
Yii
::
setAlias
(
'@Twig'
,
$this
->
twigPath
);
$loader
=
new
\Twig_Loader_String
();
$this
->
_
twig
=
new
\Twig_Environment
(
$loader
,
array_merge
(
array
(
'cache'
=>
\Yii
::
getAlias
(
$this
->
cacheDir
),
$this
->
twig
=
new
\Twig_Environment
(
$loader
,
array_merge
(
array
(
'cache'
=>
Yii
::
getAlias
(
$this
->
cachePath
),
),
$this
->
options
));
}
...
...
@@ -71,12 +67,6 @@ class TwigViewRenderer extends ViewRenderer
*/
public
function
render
(
$view
,
$file
,
$params
)
{
$ext
=
pathinfo
(
$file
,
PATHINFO_EXTENSION
);
if
(
$ext
===
$this
->
fileExtension
)
{
return
$this
->
_twig
->
render
(
file_get_contents
(
$file
),
$params
);
}
else
{
return
$view
->
renderPhpFile
(
$file
,
$params
);
}
return
$this
->
twig
->
render
(
file_get_contents
(
$file
),
$params
);
}
}
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