Add a new Twig Loader at the top of the Twig Loader chain

I want to create a new class TwigLoaderand run it in front of the Symfony class TwigLoader.

This one https://stackoverflow.com/a/2129771

I want this logic contained inside my package, so I don't want:

  • define my custom TwigLoaderinapp/config.yml
  • override symfony TwigLoader

Currently, the Symfony Twig bootloader is either returning to the default Twig boot mechanism, or if this path is similar to @Bundle:Controller:Action.format.engine, it will try to download the file @Bundle/Resources/views/Controller/Action.html.twig.

In my case, I want to be able to map @Bundle:Controller:Action.format.engineto a completely different path, and if this path does not match the existing file, it should return to the default behavior (Symfony Twig Loader).

There are probably other ways to solve the problem, but this seemed to be the most suitable way to complete the task.

I am creating a BaseThemeBundle that I will use in my open source project and want to be able to easily isolate themes from packages and potentially do all the cool stuff. :)

So far I have created a custom Twig bootloader that looks like a Symfony Twig bootloader . And I created a custom template locator .

Any ideas, solutions or improvements?

+4
1

, , , (, Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator) DI , "templating.locator" Locator.

, , (Symfony\Component\Config\FileLocatorInterface) Locator.

0

Source: https://habr.com/ru/post/1535169/


All Articles