File tree Expand file tree Collapse file tree 5 files changed +119
-0
lines changed Expand file tree Collapse file tree 5 files changed +119
-0
lines changed Original file line number Diff line number Diff line change
1
+ /vendor
2
+ /composer.lock
Original file line number Diff line number Diff line change
1
+ TwigEvalExtension
2
+ =================
3
+
4
+ One of the goals of twig is to limit the amount of logic in your templates.
5
+ Unfortunately you won't be able to do all sorts of stuff you can do with PHP.
6
+ To regain that power you can use TwigEvalExtension. Just write your php code
7
+ inside an ` eval() ` template function and it gets passed to PHP's eval and you
8
+ will get the result.
9
+
10
+ Installation
11
+ ------------
12
+
13
+ You can install the extension by using composer require.
14
+
15
+ Tests
16
+ -----
17
+
18
+ Just run
19
+
20
+ ```
21
+ phpunit tests/Twig/EvalExtensionTest.php
22
+ ```
23
+
24
+ No pesky phpunit.xml needed.
25
+
26
+ Contribution
27
+ ------------
28
+
29
+ Please don't.
30
+
31
+ Known Issues
32
+ ------------
33
+
34
+ It's eval in your templates. What could possibly go wrong?!
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " jaem3l/twig-eval-extension" ,
3
+ "description" : " Provides eval as a function in your twig templates." ,
4
+ "type" : " library" ,
5
+ "license" : " MIT" ,
6
+ "authors" : [
7
+ {
8
+ "name" : " Denis Brumann" ,
9
+ "email" : " denis.brumann@sensiolabs.de"
10
+ }
11
+ ],
12
+ "autoload" : {
13
+ "psr-4" : {
14
+ "Brumann\\ Twig\\ " : " src/Twig"
15
+ }
16
+ },
17
+ "require" : {},
18
+ "require-dev" : {
19
+ "twig/twig" : " ^2.4" ,
20
+ "phpunit/phpunit" : " ^6.4"
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Brumann \Twig ;
6
+
7
+ class EvalExtension extends \Twig_Extension
8
+ {
9
+ public function getFunctions ()
10
+ {
11
+ return [
12
+ new \Twig_Function (
13
+ 'eval ' ,
14
+ function (string $ code ) {
15
+ return eval ($ code );
16
+ }
17
+ )
18
+ ];
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Brumann \Twig \Tests ;
6
+
7
+ use Brumann \Twig \EvalExtension ;
8
+ use PHPUnit \Framework \TestCase ;
9
+
10
+ class EvalExtensionTest extends TestCase
11
+ {
12
+ public function testSimpleEval ()
13
+ {
14
+ $ templates = [
15
+ 'simple_eval ' => '{{ eval("echo 1+1;") }} ' ,
16
+ ];
17
+
18
+ $ loader = new \Twig_Loader_Array ($ templates );
19
+ $ twig = new \Twig_Environment ($ loader );
20
+ $ twig ->addExtension (new EvalExtension ());
21
+
22
+ $ output = $ twig ->render ('simple_eval ' );
23
+
24
+ $ this ->assertSame ('2 ' , $ output );
25
+ }
26
+
27
+ public function testNestedEval ()
28
+ {
29
+ $ templates = [
30
+ 'nested_eval ' => '{{ eval("eval(\"echo 1+2;\");") }} ' ,
31
+ ];
32
+
33
+ $ loader = new \Twig_Loader_Array ($ templates );
34
+ $ twig = new \Twig_Environment ($ loader );
35
+ $ twig ->addExtension (new EvalExtension ());
36
+
37
+ $ output = $ twig ->render ('nested_eval ' );
38
+
39
+ $ this ->assertSame ('3 ' , $ output );
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments