-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add callback functions/filters #13
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
<?php | ||
/** | ||
* Twig view renderer | ||
* | ||
|
@@ -96,6 +96,9 @@ function init() | |
|
||
// Adding Yii::app() object to globals | ||
$this->_twig->addGlobal('App', $app); | ||
|
||
//Adding PHP-global functions | ||
$this->_twig->addGlobal('php', new ETwigViewRendererPhpGlobal()); | ||
|
||
// Adding Yii's core static classes proxy as 'C' shortcut (usage: {{C.Html.tag(...)}}) | ||
$this->_twig->addGlobal('C', new ETwigViewRendererYiiCoreStaticClassesProxy()); | ||
|
@@ -223,6 +226,7 @@ public function getTwig() | |
private function _addCustom($classType, $elements) | ||
{ | ||
$classFunction = 'Twig_'.$classType.'_Function'; | ||
$simpleClass = 'Twig_Simple'.$classType; | ||
|
||
foreach ($elements as $name => $func) { | ||
$twigElement = null; | ||
|
@@ -236,10 +240,15 @@ private function _addCustom($classType, $elements) | |
case is_array($func) && is_string($func[0]) && isset($func[1]) && is_array($func[1]): | ||
$twigElement = new $classFunction($func[0], $func[1]); | ||
break; | ||
// Callback given | ||
case is_callable($func): | ||
$twigElement = new $simpleClass($name, $func); | ||
break; | ||
} | ||
|
||
if ($twigElement !== null) { | ||
$this->_twig->{'add'.$classType}($name, $twigElement); | ||
if($twigElement instanceof Twig_SimpleFunction || $twigElement instanceof Twig_SimpleFilter) $this->_twig->{'add'.$classType}($twigElement); | ||
else $this->_twig->{'add'.$classType}($name, $twigElement); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's concise, but a little bit long and kind of hard to read too. I thinks it's better to not save on lines here: if ($twigElement instanceof Twig_SimpleFunction || $twigElement instanceof Twig_SimpleFilter) {
$this->_twig->{'add'.$classType}($twigElement);
} else {
$this->_twig->{'add'.$classType}($name, $twigElement);
} |
||
} else { | ||
throw new CException(Yii::t('yiiext', | ||
'Incorrect options for "{classType}" [{name}]', | ||
|
@@ -320,4 +329,18 @@ function __get($className) | |
function ETwigViewRendererVoidFunction($argument) | ||
{ | ||
return ''; | ||
} | ||
|
||
/** | ||
* Class-wrapper of PHP-global functions | ||
* | ||
* @author Dmitry Morgachev <[email protected]> | ||
* @version 1.0.0 | ||
*/ | ||
class ETwigViewRendererPhpGlobal | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
And I don't think it's a wrapper. I think it's really more like a proxy class. It's passing calls through, not wrapping it. So my proposal for class name is: What do you think? |
||
{ | ||
function __call($func, $args=array()) | ||
{ | ||
return call_user_func_array($func, $args); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Twig view renderer | ||
Twig view renderer | ||
================== | ||
|
||
This extension allows you to use [Twig](http://twig.sensiolabs.org) templates in Yii. | ||
|
@@ -69,4 +69,28 @@ This extension allows you to use [Twig](http://twig.sensiolabs.org) templates in | |
] | ||
}, true) }} | ||
</div><!-- mainmenu --> | ||
``` | ||
|
||
###Callbacks usage example | ||
```php | ||
'viewRenderer' => array( | ||
'class' => 'ext.ETwigViewRenderer', | ||
'filters' => array( | ||
't' => function($message, $options = array(), $category = 'app'){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing space before opening curly brace. |
||
return Yii::t($category, $message, $options); //change places of arguments | ||
} | ||
), | ||
'functions' => array( | ||
'route' => function($route) { | ||
return Yii::app()->urlManager->createUrl($route); //use non-static methods | ||
} | ||
) | ||
), | ||
``` | ||
|
||
Than you can do that: | ||
|
||
```html | ||
<h1>{{ 'Hello, world!'|t }}</h1> | ||
<a href="{{ route('site/index') }}">{{ 'Home'|t }}</a> | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One space after
//
would be nicer.