Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions ETwigViewRenderer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
/**
* Twig view renderer
*
Expand Down Expand Up @@ -96,6 +96,9 @@ function init()

// Adding Yii::app() object to globals
$this->_twig->addGlobal('App', $app);

//Adding PHP-global functions
Copy link
Contributor

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.

$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());
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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}]',
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PhpGlobal doesn't make much sense to me. PhpGlobalFunctionsProxy - does, IMHO.
Yeah, it's longer, but more obvious in it's intentions and more correlating with other code around.

Class-wrapper of PHP-global functions

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: ETwigViewRendererPhpGlobalFunctionsProxy
And for comment: Class-proxy for PHP-global functions

What do you think?

{
function __call($func, $args=array())
{
return call_user_func_array($func, $args);
}
}
26 changes: 25 additions & 1 deletion README.md
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.
Expand Down Expand Up @@ -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'){
Copy link
Contributor

Choose a reason for hiding this comment

The 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>
```