Chris
Chris

Reputation: 7184

Can I include PHP templates in a TWIG base template or vice versa?

I am currently in the process of refactoring and restructuring an existing Symfony 2 application.

There are a few base templates and all other templates inherit from them. Right now, all the templates are PHP. I am planning to migrate the application to TWIG.

The problem is that this application is already in use and while migrating I am also expected to add new features to the application. This means I can not freeze the current state of the application, fully migrate to TWIG and then continue working with that.

This means that a migration to TWIG is only possible in small steps, i.e. I can only migrate a handful of templates before I have to deploy a new release. There will also be some new templates for the features I am adding and I would also prefer to add these in TWIG format.

My plan right now is to migrate the base templates to TWIG first and then include the existing PHP templates in those new base templates. Afterwards, I would continue migrating the other templates in small batches.

Looking at documentation online, it is possible to have both the PHP and TWIG template engines enabled simultaneously. However, I could not find any tutorials, examples or documentation mentioning that you could not only have those two engines side by side but also have templates from one engine include templates from the other engine.

Is there a way to mix templates from those two engines as I described?

Upvotes: 4

Views: 764

Answers (1)

Alain
Alain

Reputation: 37004

Even if there are similarities between both engines (they still do the very same thing), they are working internally in their own ways, the architecture and the code are very different.

Have a look to both codes, and you'll understand why we just can't compare/mix them.

For example:

  • slots from php engine plays with string overwriting
  • blocks from twig engine plays with class overloading

So even if you were able to access the twig-compiled file using a tricky import, you'll not be able to overload a block by the simple fact that your php template isn't a class.

Upvotes: 4

Related Questions