Skip to content

Commit 454be20

Browse files
committed
feat: runs addFunctions() only once
1 parent c6bba83 commit 454be20

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/CI4Twig/Twig.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class Twig
6767
*/
6868
private bool $functions_added = false;
6969

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

7277
/**
@@ -189,6 +194,7 @@ public function display($view, $params = [])
189194
public function render($view, $params = []): string
190195
{
191196
$this->createTwig();
197+
192198
// We call addFunctions() here, because we must call addFunctions()
193199
// after loading CodeIgniter functions in a controller.
194200
$this->addFunctions();
@@ -201,6 +207,11 @@ public function render($view, $params = []): string
201207

202208
protected function addFilters()
203209
{
210+
// Runs only once
211+
if ($this->filters_added) {
212+
return;
213+
}
214+
204215
foreach ($this->filters as $filter) {
205216
if (function_exists($filter)) {
206217
$this->twig->addFilter(
@@ -211,6 +222,8 @@ protected function addFilters()
211222
);
212223
}
213224
}
225+
226+
$this->filters_added = true;
214227
}
215228

216229
protected function addFunctions()

0 commit comments

Comments
 (0)