How to extend twig

If needed twig extensions can be replaced or new extension can be added to the project.

Replacing existing extensions

Replacing extension can be done in two ways.

As every extension is a service, so redefining particular service in custom services.yaml file does the job.

Adding new extension

Adding new extension is as simple as defining service. Extension is automatically added to Twig environment if service has twig.extension tag:

services:
  OxidEsales\Twig\Extensions\StyleExtension:
    class: OxidEsales\Twig\Extensions\StyleExtension
    tags: ['twig.extension']

Adding new escapers

Every escaper is a class which implements OxidEsalesTwigEscaperEscaperInterface. Escaper is registered in Twig as service with twig.escaper tag:

services:
  OxidEsales\Twig\Escaper\MailEscaper:
    class: OxidEsales\Twig\Escaper\MailEscaper
    tags: ['twig.escaper']

If needed, escapers can be overloaded by redefining services.