Ask your Symfony questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Call a twig template from a php template and viceversa Symfony


-How can I call a Twig template from within a PHP template?

For example:


<?php some_method('mytemplate.html.twig') ?>

-How can I call a PHP template from within a Twig template?

Answers (2)


Luis Cordova answers:

render a controller from within the php template and in the controller render the twig template

you can also

use or the regular twig template and render the template result into a variable then render php template and output that variable

notice one thing: TwigStringBundle has an extension that can be called within php so that will give you render powers for twig within php

Share the usecase if you can so I can also benefit and perhaps blog about it.

hope that helps!

fxsymfony comments:

Hi Luis,

Thanks for that info, I'll check that bundle.

I've been a user of Symfony 1.4 and I'm moving on to Symfony2, but I'm not enthusiastic about using Twig, I'm sure it's very powerful but since it's not a mainstream technology it doesn't feel like it's worth it to me.

I'm installing the FOSBundle at the moment and I want to customise some of the templates (using PHP), but since I have to change the bundle's engine to PHP and all of the templates come in Twig I have to find a way to use both as I don't want to rewrite all the templates.

I think I will be confronted to both cases, in this one in particular loading my custom php files from a twig template will save me work, I know in future cases I may want to load a twig template from php as well as developers seem to prefer twig when making bundles (I guess that will change as Symfony2 gets more users).

Anyway, will check that bundle out in the next couple of days and let you know.

Luis Cordova comments:

i fully advise you to do the complete jump to twig and if you need the specificity of php for some things that you create an extension or filter which is the simplest thing in the world. That is how you can still use some php yet with twig flavor. Twig is mainstream, if you are still on php you will waste a ton of time more than if you use twig. It is so simple and powerful lately even has been repowered by fabpot which is the main dev from SensioLabs. You are wasting a lot of your time sticking with that approach really, I warn you.

But of course it is my opinion, but a well acquainted opinion. Please switch you can always go back easily, i have been doing an importing from php to twig and it feels almost natural. It reduces so much of the space and the templates are so much clean and meaningful, it just rocks.

fxsymfony comments:

Thanks for the advice. I'm sure it's a great product, but the reason why I'm a bit wary about it is that it requires that extra training so it will be harder to find contractors or developers that use it. If I use php templates I can find easily people who will understand them right out of the box, and I'm talking designers, etc, maybe twig is simpler but it's more likely that it will have to be me who teaches them twig..

It's just my opinion anyway, and as such it may change dramatically as I dig into Symfony2, you are most likely right about it.


Benjamin Lévêque answers:

First, you need to enable both php and twig templating
templating: { engines: ['twig', 'php'] }

Then in your twig template you can use this:

{% include 'AcmeArticleBundle:Article:articleDetails.html.php' with {'article': article} %}

And inside you php template you can use this:
<?php echo $view->render('AcmeArticleBundle:Article:articleDetails.html.php', array('article' => $article)) ?>