Skip to content

Commit

Permalink
minor #3052 Remove docs about overloading (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.x branch.

Discussion
----------

Remove docs about overloading

Commits
-------

18e3728 removed docs about overloading
  • Loading branch information
fabpot committed Jun 4, 2019
2 parents e0ee3b5 + 18e3728 commit d4424dd
Showing 1 changed file with 0 additions and 43 deletions.
43 changes: 0 additions & 43 deletions doc/advanced.rst
Expand Up @@ -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
--------------------

Expand Down

0 comments on commit d4424dd

Please sign in to comment.