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
68079487
Commit
68079487
authored
May 01, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of git.yiisoft.com:yii2
parents
8a07bce8
ed8b5bd7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
74 additions
and
0 deletions
+74
-0
view_renderers.md
docs/view_renderers.md
+32
-0
SmartyViewRenderer.php
framework/renderers/SmartyViewRenderer.php
+34
-0
TwigViewRenderer.php
framework/renderers/TwigViewRenderer.php
+8
-0
No files found.
docs/view_renderers.md
View file @
68079487
...
@@ -18,6 +18,7 @@ array(
...
@@ -18,6 +18,7 @@ array(
),
),
'twig'
=>
array
(
'twig'
=>
array
(
'class'
=>
'yii\renderers\TwigViewRenderer'
,
'class'
=>
'yii\renderers\TwigViewRenderer'
,
'twigPath'
=>
'@app/vendors/Twig'
,
),
),
// ...
// ...
),
),
...
@@ -38,6 +39,21 @@ or `$this->renderPartial()` from your controller:
...
@@ -38,6 +39,21 @@ or `$this->renderPartial()` from your controller:
echo
$this
->
render
(
'renderer.twig'
,
array
(
'username'
=>
'Alex'
));
echo
$this
->
render
(
'renderer.twig'
,
array
(
'username'
=>
'Alex'
));
```
```
### Additional functions
Additionally to regular Twig syntax the following is available in Yii:
```
php
<a
href=
"{{ path('blog/view', {'alias' : post.alias}) }}"
>
{{ post.title }}
</a>
```
path function calls
`Html::url()`
internally.
### Additional variables
-
`app`
=
`\Yii::$app`
-
`this`
= current
`View`
object
Smarty
Smarty
------
------
...
@@ -49,3 +65,18 @@ or `$this->renderPartial()` from your controller:
...
@@ -49,3 +65,18 @@ or `$this->renderPartial()` from your controller:
```
php
```
php
echo
$this
->
render
(
'renderer.tpl'
,
array
(
'username'
=>
'Alex'
));
echo
$this
->
render
(
'renderer.tpl'
,
array
(
'username'
=>
'Alex'
));
```
```
### Additional functions
Additionally to regular Smarty syntax the following is available in Yii:
```
php
<a
href=
"{path route='blog/view' alias=$post.alias}"
>
{$post.title}
</a>
```
path function calls
`Html::url()`
internally.
### Additional variables
-
`$app`
=
`\Yii::$app`
-
`$this`
= current
`View`
object
\ No newline at end of file
framework/renderers/SmartyViewRenderer.php
View file @
68079487
...
@@ -13,6 +13,7 @@ use Yii;
...
@@ -13,6 +13,7 @@ use Yii;
use
Smarty
;
use
Smarty
;
use
yii\base\View
;
use
yii\base\View
;
use
yii\base\ViewRenderer
;
use
yii\base\ViewRenderer
;
use
yii\helpers\Html
;
/**
/**
* SmartyViewRenderer allows you to use Smarty templates in views.
* SmartyViewRenderer allows you to use Smarty templates in views.
...
@@ -48,6 +49,34 @@ class SmartyViewRenderer extends ViewRenderer
...
@@ -48,6 +49,34 @@ class SmartyViewRenderer extends ViewRenderer
$this
->
smarty
=
new
Smarty
();
$this
->
smarty
=
new
Smarty
();
$this
->
smarty
->
setCompileDir
(
Yii
::
getAlias
(
$this
->
compilePath
));
$this
->
smarty
->
setCompileDir
(
Yii
::
getAlias
(
$this
->
compilePath
));
$this
->
smarty
->
setCacheDir
(
Yii
::
getAlias
(
$this
->
cachePath
));
$this
->
smarty
->
setCacheDir
(
Yii
::
getAlias
(
$this
->
cachePath
));
$this
->
smarty
->
registerPlugin
(
'function'
,
'path'
,
array
(
$this
,
'smarty_function_path'
));
}
/**
* Smarty template function to get a path for using in links
*
* Usage is the following:
*
* {path route='blog/view' alias=$post.alias user=$user.id}
*
* where route is Yii route and the rest of parameters are passed as is.
*
* @param $params
* @param \Smarty_Internal_Template $template
*
* @return string
*/
public
function
smarty_function_path
(
$params
,
\Smarty_Internal_Template
$template
)
{
if
(
!
isset
(
$params
[
'route'
]))
{
trigger_error
(
"path: missing 'route' parameter"
);
}
array_unshift
(
$params
,
$params
[
'route'
])
;
unset
(
$params
[
'route'
]);
return
Html
::
url
(
$params
);
}
}
/**
/**
...
@@ -67,6 +96,10 @@ class SmartyViewRenderer extends ViewRenderer
...
@@ -67,6 +96,10 @@ class SmartyViewRenderer extends ViewRenderer
$ext
=
pathinfo
(
$file
,
PATHINFO_EXTENSION
);
$ext
=
pathinfo
(
$file
,
PATHINFO_EXTENSION
);
/** @var \Smarty_Internal_Template $template */
/** @var \Smarty_Internal_Template $template */
$template
=
$this
->
smarty
->
createTemplate
(
$file
,
null
,
null
,
$params
,
true
);
$template
=
$this
->
smarty
->
createTemplate
(
$file
,
null
,
null
,
$params
,
true
);
$template
->
assign
(
'app'
,
\Yii
::
$app
);
$template
->
assign
(
'this'
,
$view
);
return
$template
->
fetch
();
return
$template
->
fetch
();
}
}
}
}
\ No newline at end of file
framework/renderers/TwigViewRenderer.php
View file @
68079487
...
@@ -12,6 +12,7 @@ namespace yii\renderers;
...
@@ -12,6 +12,7 @@ namespace yii\renderers;
use
Yii
;
use
Yii
;
use
yii\base\View
;
use
yii\base\View
;
use
yii\base\ViewRenderer
;
use
yii\base\ViewRenderer
;
use
yii\helpers\Html
;
/**
/**
* TwigViewRenderer allows you to use Twig templates in views.
* TwigViewRenderer allows you to use Twig templates in views.
...
@@ -53,6 +54,12 @@ class TwigViewRenderer extends ViewRenderer
...
@@ -53,6 +54,12 @@ class TwigViewRenderer extends ViewRenderer
$this
->
twig
=
new
\Twig_Environment
(
$loader
,
array_merge
(
array
(
$this
->
twig
=
new
\Twig_Environment
(
$loader
,
array_merge
(
array
(
'cache'
=>
Yii
::
getAlias
(
$this
->
cachePath
),
'cache'
=>
Yii
::
getAlias
(
$this
->
cachePath
),
),
$this
->
options
));
),
$this
->
options
));
$this
->
twig
->
addFunction
(
'path'
,
new
\Twig_Function_Function
(
function
(
$path
,
$args
=
array
()){
return
Html
::
url
(
array_merge
(
array
(
$path
),
$args
));
}));
$this
->
twig
->
addGlobal
(
'app'
,
\Yii
::
$app
);
}
}
/**
/**
...
@@ -69,6 +76,7 @@ class TwigViewRenderer extends ViewRenderer
...
@@ -69,6 +76,7 @@ class TwigViewRenderer extends ViewRenderer
*/
*/
public
function
render
(
$view
,
$file
,
$params
)
public
function
render
(
$view
,
$file
,
$params
)
{
{
$this
->
twig
->
addGlobal
(
'this'
,
$view
);
return
$this
->
twig
->
render
(
file_get_contents
(
$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