Skip to content

Commit e2d85f6

Browse files
authored
Merge pull request #65 from kenjis/feat-filter-support
feat: filter support
2 parents ca00400 + 820962f commit e2d85f6

File tree

5 files changed

+84
-23
lines changed

5 files changed

+84
-23
lines changed

ChangeLog.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Change Log for CodeIgniter Simple and Secure Twig
22

3+
## v4.2.0 (Unreleased)
4+
5+
### Changed
6+
7+
- Require:
8+
- PHP 7.4 or later.
9+
- Twig 3.4.3 or later.
10+
- CodeIgniter 4.2.11 or later.
11+
- Add functionality to add your filters. See [README](README.md#adding-your-functions--filters).
12+
313
## v4.1.0 (2022-09-20)
414

515
### Changed
@@ -51,7 +61,7 @@
5161

5262
### Added
5363

54-
* Add functionality to add your functions. See [README](README.md#adding-your-functions).
64+
* Add functionality to add your functions. See [README](README.md#adding-your-functions--filters).
5565

5666
## v0.1.0 (2015/11/17)
5767

README.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ If you use CodeIgniter3, check [master](https://github.com/kenjis/codeigniter-ss
88

99
## Requirements
1010

11-
* PHP 7.3 or later
12-
* CodeIgniter 4.2 or later
13-
* Twig 3.3.8 or later
11+
* PHP 7.4 or later
12+
* CodeIgniter 4.2.11 or later
13+
* Twig 3.4.3 or later
1414

1515
## Installation
1616

@@ -33,8 +33,8 @@ You can override the default configuration:
3333

3434
~~~php
3535
$config = [
36-
'paths' => ['/path/to/twig/templates', VIEWPATH],
37-
'cache' => '/path/to/twig/cache',
36+
'paths' => ['/path/to/twig/templates', VIEWPATH],
37+
'cache' => '/path/to/twig/cache',
3838
];
3939
$this->twig = new \Kenjis\CI4Twig\Twig($config);
4040
~~~
@@ -71,25 +71,26 @@ $twig = $this->twig->getTwig();
7171

7272
### Supported CodeIgniter Helpers
7373

74-
* `base_url`
75-
* `site_url`
76-
* `anchor`
77-
* `form_open`
78-
* `form_close`
79-
* `form_error`
80-
* `form_hidden`
81-
* `set_value`
74+
* `base_url()`
75+
* `site_url()`
76+
* `anchor()`
77+
* `form_open()`
78+
* `form_close()`
79+
* `form_error()`
80+
* `form_hidden()`
81+
* `set_value()`
8282

8383
Some helpers are added the functionality of auto-escaping for security.
8484

85-
### Adding Your Functions
85+
### Adding Your Functions & Filters
8686

87-
You can add your functions with configuration:
87+
You can add your functions and filters with configuration:
8888

8989
~~~php
9090
$config = [
91-
'functions' => ['my_helper'],
92-
'functions_safe' => ['my_safe_helper'],
91+
'functions' => ['my_helper'],
92+
'functions_safe' => ['my_safe_helper'],
93+
'filters' => ['my_filter'],
9394
];
9495
$this->twig = new \Kenjis\CI4Twig\Twig($config);
9596
~~~

src/CI4Twig/Twig.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Twig\Error\SyntaxError;
1717
use Twig\Extension\DebugExtension;
1818
use Twig\Loader\FilesystemLoader;
19+
use Twig\TwigFilter;
1920
use Twig\TwigFunction;
2021

2122
class Twig
@@ -40,6 +41,11 @@ class Twig
4041
'site_url',
4142
];
4243

44+
/**
45+
* @var array Filters to add to Twig
46+
*/
47+
private array $filters = [];
48+
4349
/**
4450
* @var array Functions with `is_safe` option
4551
*
@@ -61,6 +67,11 @@ class Twig
6167
*/
6268
private bool $functions_added = false;
6369

70+
/**
71+
* @var bool Whether filters are added or not
72+
*/
73+
private bool $filters_added = false;
74+
6475
private ?Environment $twig = null;
6576

6677
/**
@@ -87,6 +98,13 @@ public function __construct($params = [])
8798
unset($params['functions_safe']);
8899
}
89100

101+
if (isset($params['filters'])) {
102+
$this->filters = array_unique(
103+
array_merge($this->filters, $params['filters'])
104+
);
105+
unset($params['filters']);
106+
}
107+
90108
if (isset($params['paths'])) {
91109
$this->paths = $params['paths'];
92110
unset($params['paths']);
@@ -176,15 +194,35 @@ public function display($view, $params = [])
176194
public function render($view, $params = []): string
177195
{
178196
$this->createTwig();
197+
179198
// We call addFunctions() here, because we must call addFunctions()
180199
// after loading CodeIgniter functions in a controller.
181200
$this->addFunctions();
201+
$this->addFilters();
182202

183203
$view = $view . '.twig';
184204

185205
return $this->twig->render($view, $params);
186206
}
187207

208+
protected function addFilters()
209+
{
210+
// Runs only once
211+
if ($this->filters_added) {
212+
return;
213+
}
214+
215+
foreach ($this->filters as $filter) {
216+
if (function_exists($filter)) {
217+
$this->twig->addFilter(
218+
new TwigFilter($filter, $filter)
219+
);
220+
}
221+
}
222+
223+
$this->filters_added = true;
224+
}
225+
188226
protected function addFunctions()
189227
{
190228
// Runs only once
@@ -196,10 +234,7 @@ protected function addFunctions()
196234
foreach ($this->functions_asis as $function) {
197235
if (function_exists($function)) {
198236
$this->twig->addFunction(
199-
new TwigFunction(
200-
$function,
201-
$function
202-
)
237+
new TwigFunction($function, $function)
203238
);
204239
}
205240
}

tests/CI4Twig/TwigTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function setUpBeforeClass(): void
1919
helper('form');
2020
}
2121

22-
public function testRedner()
22+
public function testRender()
2323
{
2424
$obj = new Twig(['paths' => __DIR__ . '/../templates/']);
2525

@@ -110,4 +110,18 @@ public function testFunctionSafe()
110110
$output = $obj->render('functions_safe');
111111
$this->assertSame("<s>test</s>\n", $output);
112112
}
113+
114+
public function testFilter()
115+
{
116+
$obj = new Twig(
117+
[
118+
'paths' => __DIR__ . '/../templates/',
119+
'filters' => ['str_rot13'],
120+
'cache' => false,
121+
]
122+
);
123+
124+
$output = $obj->render('filters');
125+
$this->assertSame("PbqrVtavgre Fvzcyr naq Frpher Gjvt\n", $output);
126+
}
113127
}

tests/templates/filters.twig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ 'CodeIgniter Simple and Secure Twig'|str_rot13 }}

0 commit comments

Comments
 (0)