-
-
Notifications
You must be signed in to change notification settings - Fork 61
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
Added support for callable as a source for Twig_Function and Twig_Filter #15
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
How to use it? |
Also it seems it breaks tests: https://travis-ci.org/yiisoft/yii2-twig/jobs/56381597#L158 |
Here is how I use it, place this to configuration array: 'components'=>[
'view' => [
'renderers' => [
'twig' => [
'class' => 'yii\twig\ViewRenderer',
// set cachePath to false in order to disable template caching
'cachePath' => '@runtime/Twig/cache',
// Array of twig options:
'options' => [
'auto_reload' => true,
'autoescape' => true,
//'debug' => true,
],
// ... see ViewRenderer for more options
'extensions'=>[
// In order to make dump() to output to the screen, we also need to set options[debug] = true.
'Twig_Extension_Debug',
],
'globals'=>[
'html' => 'CHtml'
],
'functions'=>[
'staticCall'=>'call_user_func_array',
'static'=>function ($class, $property) {
return property_exists($class, $property)
// Static variable.
? $class::$$property
: (defined("$class::$property")
// Class constant.
? constant("$class::$property")
: null);
},
'isInstanceOf'=>function($object,$className) {
return ($object instanceof $className);
},
'setProperty'=>function($object,$key,$value) {
// In order to tame low-level error, which is very hard to debug otherwise - only "Creating default object from empty value".
if (empty($object)) {
throw new CException('Trying to set a property of empty object');
}
$object->{$key} = $value;
},
'unset'=>function($variable) {
unset($variable);
},
'arrayUnset'=>function($array, $key) {
unset($array[$key]);
return $array;
},
'new'=>function($className,$args=[]) {
// Only the first 3 parameters - for simplicity and safe.
$args = (array)$args;
$arg1 = array_shift($args);
$args = (array)$args;
$arg2 = array_shift($args);
$args = (array)$args;
$arg3 = array_shift($args);
$args = (array)$args;
if ($arg3) {
return new $className($arg1,$arg2,$arg3);
}
elseif ($arg2) {
return new $className($arg1,$arg2);
}
elseif ($arg1) {
return new $className($arg1);
}
else {
return new $className();
}
},
// To debug deep places of Twig templates.
'throw'=>function(Exception $exception) {
throw $exception;
},
// Helper.
'print_r' => function($value, $return=true) {
return print_r($value,$return);
},
],
],
],
],
], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See yiiext/twig-renderer#32 .