How do I include raw HTML files in Symfony2/Twig templates? How do I include raw HTML files in Symfony2/Twig templates? symfony symfony

How do I include raw HTML files in Symfony2/Twig templates?


I also came upon the same problem trying to find a solution to include files (mustache templates) as raw in Twig templates so Twig doesn't try to parse them.

At first I had my mustache template files named simply sometemplate.html and wrapped in {% raw %} tags. This worked for a while, but then I started using PhpStorm IDE with the Handlebars plugin (for mustache syntax). For PhpStorm to recognize the files as mustache syntax, they need to have a unique file extension (.mustache by default), so I renamed my sometemplate.html to sometemplate.mustache but I really disliked the idea that my mustache templates needed to be wrapped with Twig tags. So I ended up doing what @rdjs said in his option 3. This is the best solution imo.

Here's the working Twig extension function I made:

function twig_include_raw(Twig_Environment $env, $template) {    return $env->getLoader()->getSource($template);}$twig->addFunction('include_raw', new Twig_Function_Function('twig_include_raw', array('needs_environment' => true)));

With this in place you can easily include files as "raw" without Twig parsing them by doing:

{{ include_raw('sometemplate.mustache')|raw }}

I even made a Twig macro for simplifying including mustache templates to HTML head sections:

{% macro mustache_script(id, file) -%}<script id="{{ id }}" type="text/x-mustache-template">{{ include_raw(file)|raw }}</script>{%- endmacro %}

And after importing the file with the above macro to your Twig template ({% import "macros.twig" %} for example), you can easily import mustache template files in your Twig templates by simply doing {{ mustache_script('sometemplate_tpl', 'sometemplate.mustache') }} inside a HTML <head> section.

I hope this helps someone who's looking for a solution to the same problem.


A quick recap on twig file extensions (taken from the documentation):

Every template name also has two extensions that specify the format and engine for that template.

AcmeBlogBundle:Blog:index.html.twig - HTML format, Twig engineAcmeBlogBundle:Blog:index.html.php - HTML format, PHP engineAcmeBlogBundle:Blog:index.css.twig - CSS format, Twig engine

By default, any Symfony2 template can be written in either Twig or PHP, and the last part of the extension (e.g. .twig or .php) specifies which of these two engines should be used. The first part of the extension, (e.g. .html, .css, etc) is the final format that the template will generate.

Therefore it makes sense to me that including a file as .html would be at the least ambiguous even if it didn't throw an error.

So you have 3 choices:

  1. If the files are purely javascript then include them as script tags in your page.

  2. If they are mixed HTML and JS then escape the JS with {% raw %} and include the files as foo.html.twig templates. If there are lots of scripts being included like this then most likely your designers could do with a little refactoring and move the bulk of their scripts to external files (see option 1)

  3. If you really insist you could always write a Twig extension to include raw HTML files. (EDIT: See @Haprog's answer below for more details on this option).

    {{ include_html('foo/bar.html') }}

  4. UPDATE 2015 twig has since added the source function:

{{ source('AcmeSomeBundle:Default:somefile.html.twig') }}

Kudos to @Nigel Angel in the comments below for option 4.


I came accross this post, as I had a similar question. After an hour or so searching and trying, I found out, that as from Twig Version 1.15 the "source Function" was added.

Maybe that helps someone in the future.