Skip to content

Commit 15dd9f4

Browse files
committed
Merge pull request #8 from kenjis/add-functions
Add functionality to add functions with config
2 parents ba3fa96 + acef5df commit 15dd9f4

File tree

5 files changed

+80
-20
lines changed

5 files changed

+80
-20
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Remove comment marks below and fix the path for `Autoloader.php`:
7171
$this->load->library('twig');
7272
~~~
7373

74-
You can override the default configration:
74+
You can override the default configuration:
7575

7676
~~~php
7777
$config = [
@@ -126,6 +126,20 @@ $twig = $this->twig->getTwig();
126126

127127
Some helpers are added the functionality of auto-escaping for security.
128128

129+
### Adding Your Functions
130+
131+
You can add your functions with configuration:
132+
133+
~~~php
134+
$config = [
135+
'functions' => ['my_helper'],
136+
'functions_safe' => ['my_safe_helper'],
137+
];
138+
$this->load->library('twig', $config);
139+
~~~
140+
141+
If your function explicitly outputs HTML code, you will want the raw output to be printed. In such a case, use `functions_safe`, and you have to make sure the output of the function is XSS free.
142+
129143
### Reference
130144

131145
#### Documentation

libraries/Twig.php

Lines changed: 23 additions & 8 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']))
54+
{
55+
$this->functions_asis =
56+
array_unique(
57+
array_merge($this->functions_asis, $params['functions'])
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
/**
@@ -198,7 +213,7 @@ protected function addCIFunctions()
198213
* @param array $attributes [changed] only array is acceptable
199214
* @return string
200215
*/
201-
protected function safe_anchor($uri = '', $title = '', $attributes = [])
216+
public function safe_anchor($uri = '', $title = '', $attributes = [])
202217
{
203218
$uri = html_escape($uri);
204219
$title = html_escape($title);

tests/libraries/TwigTest.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static function setUpBeforeClass()
66
{
77
parent::setUpBeforeClass();
88
$CI =& get_instance();
9-
$CI->load->library('Twig');
9+
$CI->load->library('twig');
1010
$CI->load->helper('url_helper');
1111
$CI->load->helper('form_helper');
1212
}
@@ -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' => ['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)