Skip to content
Closed
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
20 changes: 10 additions & 10 deletions ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,26 +230,26 @@ public function setLexerOptions($options)
*/
private function _addCustom($classType, $elements)
{
$classFunction = 'Twig_' . $classType . '_Function';
$classFunction = 'Twig_Simple' . $classType;

foreach ($elements as $name => $func) {
$twigElement = null;

switch ($func) {
// Just a name of function
case is_string($func):
$twigElement = new $classFunction($func);
// Callable (including just a name of function).
case is_callable($func):
$twigElement = new $classFunction($name, $func);
break;
// Name of function + options array
case is_array($func) && is_string($func[0]) && isset($func[1]) && is_array($func[1]):
$twigElement = new $classFunction($func[0], $func[1]);
// Callable (including just a name of function) + options array.
case is_array($func) && is_callable($func[0]):
$twigElement = new $classFunction($name, $func[0], (!empty($func[1]) && is_array($func[1])) ? $func[1] : []);
break;
case $func instanceof \Twig_SimpleFunction || $func instanceof \Twig_SimpleFilter:
$twigElement = $func;
}

if ($twigElement !== null) {
$this->twig->{'add'.$classType}($name, $twigElement);
} elseif ($func instanceof \Twig_SimpleFunction || $func instanceof \Twig_SimpleFilter) {
$this->twig->{'add'.$classType}($func);
$this->twig->{'add'.$classType}($twigElement);
} else {
throw new \Exception("Incorrect options for \"$classType\" $name.");
}
Expand Down
20 changes: 18 additions & 2 deletions docs/guide/additional-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ You can define additional functions like the following:
new \Twig_SimpleFunction('rot14', 'str_rot13'),
new \Twig_SimpleFunction('add_*', function ($symbols, $val) {
return $val . $symbols;
}, ['is_safe' => ['html']])
}, ['is_safe' => ['html']]),
'callable_add_*' => function ($symbols, $val) {
return $val . $symbols;
},
'sum' => function ($a, $b) {
return $a + $b;
}
],
```

Expand All @@ -47,6 +53,8 @@ In template they could be used like the following:
`{{ truncate(post.text, 100) }}`
`{{ rot14('test') }}`
`{{ add_42('answer') }}`
`{{ callable_add_42('test') }}`
`{{ sum(1, 2) }}`
```

## Filters
Expand All @@ -59,7 +67,13 @@ Additional filters may be added via the application configuration's `filters` op
new \Twig_SimpleFilter('rot13', 'str_rot13'),
new \Twig_SimpleFilter('add_*', function ($symbols, $val) {
return $val . $symbols;
}, ['is_safe' => ['html']])
}, ['is_safe' => ['html']]),
'callable_rot13' => function($string) {
return str_rot13($string);
},
'callable_add_*' => function ($symbols, $val) {
return $val . $symbols;
}
],
```

Expand All @@ -69,4 +83,6 @@ Then in the template you can apply filter using the following syntax:
{{ model|jsonEncode }}
{{ 'test'|rot13 }}
{{ 'answer'|add_42 }}
{{ 'test'|callable_rot13 }}
{{ 'answer'|callable_add_42 }}
```
32 changes: 30 additions & 2 deletions tests/ViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ public function testSimpleFilters()
$this->assertEquals($content, 'Gjvt');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFilters2.twig');
$this->assertEquals($content, 'val42');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFilters3.twig');
$this->assertEquals($content, 'Gjvt');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFilters4.twig');
$this->assertEquals($content, 'val42');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFilters5.twig');
$this->assertEquals($content, 'Gjvt');
}

public function testSimpleFunctions()
Expand All @@ -126,6 +132,12 @@ public function testSimpleFunctions()
$this->assertEquals($content, 'Gjvt');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFunctions2.twig');
$this->assertEquals($content, 'val43');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFunctions3.twig');
$this->assertEquals($content, 'Gjvt');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFunctions4.twig');
$this->assertEquals($content, 'val43');
$content = $view->renderFile('@yiiunit/extensions/twig/views/simpleFunctions5.twig');
$this->assertEquals($content, '6');
}

/**
Expand All @@ -151,13 +163,29 @@ protected function mockView()
new \Twig_SimpleFunction('rot13', 'str_rot13'),
new \Twig_SimpleFunction('add_*', function ($symbols, $val) {
return $val . $symbols;
}, ['is_safe' => ['html']])
}, ['is_safe' => ['html']]),
'callable_rot13' => function($string) {
return str_rot13($string);
},
'callable_add_*' => function ($symbols, $val) {
return $val . $symbols;
},
'callable_sum' => function ($a, $b) {
return $a + $b;
}
],
'filters' => [
'string_rot13' => 'str_rot13',
new \Twig_SimpleFilter('rot13', 'str_rot13'),
new \Twig_SimpleFilter('add_*', function ($symbols, $val) {
return $val . $symbols;
}, ['is_safe' => ['html']])
}, ['is_safe' => ['html']]),
'callable_rot13' => function($string) {
return str_rot13($string);
},
'callable_add_*' => function ($symbols, $val) {
return $val . $symbols;
}
],
'lexerOptions' => [
'tag_comment' => [ '{*', '*}' ],
Expand Down
1 change: 1 addition & 0 deletions tests/views/simpleFilters3.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'Twig'|callable_rot13 }}
1 change: 1 addition & 0 deletions tests/views/simpleFilters4.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'val'|callable_add_42 }}
1 change: 1 addition & 0 deletions tests/views/simpleFilters5.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ 'Twig'|string_rot13 }}
1 change: 1 addition & 0 deletions tests/views/simpleFunctions3.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ callable_rot13('Twig') }}
1 change: 1 addition & 0 deletions tests/views/simpleFunctions4.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ callable_add_43('val') }}
1 change: 1 addition & 0 deletions tests/views/simpleFunctions5.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ callable_sum(2, 4) }}