Skip to content

Commit a9c65eb

Browse files
committed
Add functionality to add functions with config
1 parent ba3fa96 commit a9c65eb

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

libraries/Twig.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ class Twig
2626
];
2727

2828
/**
29-
* @var bool Whether added CodeIgniter functions or not
29+
* @var bool Whether functions are added or not
3030
*/
31-
private $add_ci_functions = FALSE;
31+
private $functions_added = FALSE;
3232

3333
/**
3434
* @var Twig_Environment
@@ -49,6 +49,21 @@ public function __construct($params = [])
4949
];
5050

5151
$this->config = array_merge($this->config, $params);
52+
53+
if (isset($params['functions_asis']))
54+
{
55+
$this->functions_asis =
56+
array_unique(
57+
array_merge($this->functions_asis, $params['functions_asis'])
58+
);
59+
}
60+
if (isset($params['functions_safe']))
61+
{
62+
$this->functions_safe =
63+
array_unique(
64+
array_merge($this->functions_safe, $params['functions_safe'])
65+
);
66+
}
5267
}
5368

5469
protected function resetTwig()
@@ -132,18 +147,18 @@ public function display($view, $params = [])
132147
public function render($view, $params = [])
133148
{
134149
$this->createTwig();
135-
// We call addCIFunctions() here, because we must call addCIFunctions()
150+
// We call addFunctions() here, because we must call addFunctions()
136151
// after loading CodeIgniter functions in a controller.
137-
$this->addCIFunctions();
152+
$this->addFunctions();
138153

139154
$view = $view . '.twig';
140155
return $this->twig->render($view, $params);
141156
}
142157

143-
protected function addCIFunctions()
158+
protected function addFunctions()
144159
{
145160
// Runs only once
146-
if ($this->add_ci_functions)
161+
if ($this->functions_added)
147162
{
148163
return;
149164
}
@@ -189,7 +204,7 @@ protected function addCIFunctions()
189204
);
190205
}
191206

192-
$this->add_ci_functions = TRUE;
207+
$this->functions_added = TRUE;
193208
}
194209

195210
/**

tests/libraries/TwigTest.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testAddGlobal()
4444
$this->assertEquals('<title>Twig Test Site</title>'."\n", $output);
4545
}
4646

47-
public function testAddCIFunctionsRunsOnlyOnce()
47+
public function testAddFunctionsRunsOnlyOnce()
4848
{
4949
$obj = new Twig(['paths' => __DIR__ . '/../templates/']);
5050

@@ -53,26 +53,55 @@ public function testAddCIFunctionsRunsOnlyOnce()
5353
];
5454

5555
$ref_obj = new ReflectionObject($obj);
56-
$ref_property = $ref_obj->getProperty('add_ci_functions');
56+
$ref_property = $ref_obj->getProperty('functions_added');
5757
$ref_property->setAccessible(true);
58-
$add_ci_functions = $ref_property->getValue($obj);
59-
$this->assertEquals(false, $add_ci_functions);
58+
$functions_added = $ref_property->getValue($obj);
59+
$this->assertEquals(false, $functions_added);
6060

6161
$output = $obj->render('welcome', $data);
6262

6363
$ref_obj = new ReflectionObject($obj);
64-
$ref_property = $ref_obj->getProperty('add_ci_functions');
64+
$ref_property = $ref_obj->getProperty('functions_added');
6565
$ref_property->setAccessible(true);
66-
$add_ci_functions = $ref_property->getValue($obj);
67-
$this->assertEquals(true, $add_ci_functions);
66+
$functions_added = $ref_property->getValue($obj);
67+
$this->assertEquals(true, $functions_added);
6868

6969
// Calls render() twice
7070
$output = $obj->render('welcome', $data);
7171

7272
$ref_obj = new ReflectionObject($obj);
73-
$ref_property = $ref_obj->getProperty('add_ci_functions');
73+
$ref_property = $ref_obj->getProperty('functions_added');
7474
$ref_property->setAccessible(true);
75-
$add_ci_functions = $ref_property->getValue($obj);
76-
$this->assertEquals(true, $add_ci_functions);
75+
$functions_added = $ref_property->getValue($obj);
76+
$this->assertEquals(true, $functions_added);
7777
}
78+
79+
public function testFunctionAsIs()
80+
{
81+
$obj = new Twig([
82+
'paths' => __DIR__ . '/../templates/',
83+
'functions_asis' => ['md5'],
84+
'cache' => false,
85+
]);
86+
87+
$output = $obj->render('functions_asis');
88+
$this->assertEquals('900150983cd24fb0d6963f7d28e17f72'."\n", $output);
89+
}
90+
91+
public function testFunctionSafe()
92+
{
93+
$obj = new Twig([
94+
'paths' => __DIR__ . '/../templates/',
95+
'functions_safe' => ['test_safe'],
96+
'cache' => false,
97+
]);
98+
99+
$output = $obj->render('functions_safe');
100+
$this->assertEquals('<s>test</s>'."\n", $output);
101+
}
102+
}
103+
104+
function test_safe()
105+
{
106+
return '<s>test</s>';
78107
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ md5('abc') }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ test_safe() }}

0 commit comments

Comments
 (0)