jrdn
jrdn

Reputation: 840

Twig / Symfony 2.3.4: Extending twig template stored in database

I am currently building a CMS in Symfony 2.3.4, and as part of this CMS I would like to store all of the twig templates in a database. I've read up on how to store and access them in the database, and it seems simple enough - however, one question remains. It must be possible to extend templates stored in the database, but I have no idea how to do this.

If anybody has any experience or knowledge I would be very grateful.

Cheers!

Upvotes: 2

Views: 1197

Answers (2)

jrdn
jrdn

Reputation: 840

Following the guides at the symfony forum and the Twig Recipes Page I was able to store all the templates in the database, and extending works automatically. Make sure that your isFresh and getCacheKey return values though, otherwise you will get an infinite recursion for some reason.

It is also worth noting that you must add a tag to the database loader service, instead of adding the chain_loader service as the symfony forum suggests.

vendor.bundle.twig_database_loader:
    class: Vendor\Bundle\Twig\TwigDatabaseLoader
    arguments: [ @doctrine.orm.entity_manager ]
    tags:
        - { name: twig.loader }

This code will work quite nicely.

Upvotes: 1

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52483

You may want to dive into Twig_TokenParser_Extends to get an idea of how the extends tag works internally.

Upvotes: 1

Related Questions