Reputation: 145
So I've created a few twig loaders in my project for templates stored in databases. Simply having them implement LoaderInterface already automatically adds them to the default Twig\Loader\ChainLoader - however, I haven't found a way to configure the order.
When I check which loaders are in the ChainLoader, it's
LoaderA -> LoaderB -> FilesystemLoader
, but I want the order to be
FilesystemLoader -> LoaderB -> LoaderA
If I define twig.loader in my services.yaml, it always ends up with infinite recursion and nothing works. If I manually configure the calls to twig.loader.chain, I get the correct order of loaders, followed by a chainloader X, followed by the loaders in the "autoconfigured" order. Chainloader X is the same as the very ChainLoader Symfony configures, so... infinite recursion if a template is not found.
So how on earth do I tell twig which loaders to load in what order? Do I need to create a CompilerPass just for this simple requirement?
Upvotes: 1
Views: 775
Reputation: 8171
You can alter the order by tagging the services manually, when priority
is not specified it defaults to 0
:
# services.yaml
App\Twig\LoaderA:
tags:
- { name: 'twig.loader', priority: 2 }
App\Twig\LoaderB:
tags:
- { name: 'twig.loader', priority: 1 }
If you prefer, since Symfony 5.3 (running under PHP 8), you can use PHP attributes instead.
Upvotes: 1