From f5846bc7237bd833f73cbd2673cb665dc02a9884 Mon Sep 17 00:00:00 2001 From: Dmitry Morgachev Date: Tue, 5 Mar 2013 17:47:09 +0400 Subject: [PATCH 1/3] Added callback functions/filters --- ETwigViewRenderer.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ETwigViewRenderer.php b/ETwigViewRenderer.php index d460392..ba070d7 100644 --- a/ETwigViewRenderer.php +++ b/ETwigViewRenderer.php @@ -223,6 +223,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 +237,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); } else { throw new CException(Yii::t('yiiext', 'Incorrect options for "{classType}" [{name}]', From fd3d7a493d9f4e08fad5c627df9b82b1ae110589 Mon Sep 17 00:00:00 2001 From: izonder Date: Thu, 7 Mar 2013 14:08:33 +0400 Subject: [PATCH 2/3] Add callbacks using instructions to README --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7ee9e0f..fde1c47 100644 --- a/README.md +++ b/README.md @@ -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) }} +``` + +###Callbacks usage example +```php +'viewRenderer' => array( + 'class' => 'ext.ETwigViewRenderer', + 'filters' => array( + 't' => function($message, $options = array(), $category = 'app'){ + 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 +

{{ 'Hello, world!'|t }}

+{{ 'Home'|t }} ``` \ No newline at end of file From 5514130200d291e08f16c6584b3752d54728465c Mon Sep 17 00:00:00 2001 From: izonder Date: Wed, 13 Mar 2013 15:05:40 +0400 Subject: [PATCH 3/3] Added global PHP functions wrapper --- ETwigViewRenderer.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ETwigViewRenderer.php b/ETwigViewRenderer.php index ba070d7..61ab417 100644 --- a/ETwigViewRenderer.php +++ b/ETwigViewRenderer.php @@ -1,4 +1,4 @@ -_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()); @@ -326,4 +329,18 @@ function __get($className) function ETwigViewRendererVoidFunction($argument) { return ''; +} + +/** + * Class-wrapper of PHP-global functions + * + * @author Dmitry Morgachev + * @version 1.0.0 + */ +class ETwigViewRendererPhpGlobal +{ + function __call($func, $args=array()) + { + return call_user_func_array($func, $args); + } } \ No newline at end of file