mirror of
https://github.com/twigphp/Twig.git
synced 2026-08-30 12:06:56 +00:00
removed docs about overloading
This commit is contained in:
@@ -908,49 +908,6 @@ It is now possible to move the runtime logic to a new
|
||||
}
|
||||
}
|
||||
|
||||
Overloading
|
||||
-----------
|
||||
|
||||
To overload an already defined filter, test, operator, global variable, or
|
||||
function, re-define it in an extension and register it **as late as
|
||||
possible** (order matters)::
|
||||
|
||||
class MyCoreExtension extends \Twig\Extension\AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return [
|
||||
new \Twig\TwigFilter('date', [$this, 'dateFilter']),
|
||||
];
|
||||
}
|
||||
|
||||
public function dateFilter($timestamp, $format = 'F j, Y H:i')
|
||||
{
|
||||
// do something different from the built-in date filter
|
||||
}
|
||||
}
|
||||
|
||||
$twig = new \Twig\Environment($loader);
|
||||
$twig->addExtension(new MyCoreExtension());
|
||||
|
||||
Here, we have overloaded the built-in ``date`` filter with a custom one.
|
||||
|
||||
If you do the same on the ``\Twig\Environment`` itself, beware that it takes
|
||||
precedence over any other registered extensions::
|
||||
|
||||
$twig = new \Twig\Environment($loader);
|
||||
$twig->addFilter(new \Twig\TwigFilter('date', function ($timestamp, $format = 'F j, Y H:i') {
|
||||
// do something different from the built-in date filter
|
||||
}));
|
||||
// the date filter will come from the above registration, not
|
||||
// from the registered extension below
|
||||
$twig->addExtension(new MyCoreExtension());
|
||||
|
||||
.. caution::
|
||||
|
||||
Note that overloading the built-in Twig elements is not recommended as it
|
||||
might be confusing.
|
||||
|
||||
Testing an Extension
|
||||
--------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user