I would like to load certain Twig templates from the database in my Symfony2 application, while still keeping the possibility to use the native loader to render templates in the standard filesystem locations. How to achieve this?
As far as I've understood, there is no possibility to register multiple loaders to Twig environment. I have been thinking two ways (for now):
Update 1:
It seems that Twig supports Twig_Loader_Chain class that could be used in my first option. Still, the default loader should be accessible and passed to the chain as the first option.
To use Twig_Loader_Chain you need Symfony 2.2
https://github.com/symfony/symfony/pull/6131
Then you can simply configurate your loaders:
services:
twig.loader.filesystem:
class: %twig.loader.filesystem.class%
arguments:
- @templating.locator
- @templating.name_parser
tags:
- { name: twig.loader }
twig.loader.string:
class: %twig.loader.string.class%
tags:
- { name: twig.loader }
Update:
It looks like there are still some problems(the filesystem loader couldn't find the templates somtimes) but I found this:
http://forum.symfony-project.org/viewtopic.php?t=40382&p=131254
Seems to be working great!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With