-
Suppose I have a bit of code to use in Twig as a custom Extension, and I have it defined twice, once as a Function, the other as a Filter. So inside Twig I can use both of these
What is the proper way of creating these Extensions without duplicating the code that implements myExt? In other words, how can I call my custom Function from inside my custom Filter, or vice-versa? Thanks in advance for any guidance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
A Twig extension can totally reuse the same PHP callable to implement a Twig filter and a Twig function: <?php
// use statements omitted for brevity
class MyExtension extends AbstractExtension
{
public function getFilters(): array
{
return [new TwigFilter('myExt', $this->myExt(...)];
}
public function getFunctions(): array
{
return [new TwigFunction('myExt', $this->myExt(...)];
}
public function myExt($arg1, $arg2)
{
// TODO implement the logic
}
} |
Beta Was this translation helpful? Give feedback.
A Twig extension can totally reuse the same PHP callable to implement a Twig filter and a Twig function: