Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm working on a project in Symfony2 and I have several small pieces of html that need to be included in one of my main views. According to the official Twig documentation I should be able to simply use {% include 'filename.html' %} but in Symfony unless the filename ends in ".html.twig" it throws and error saying it cannot find the file. I'd like to avoid using Twig templates for these files since they have no dynamic content and lots of double braces (they're javascript templates) and requiring the template designer to have to wrap every one of these files in {% raw %} tags seems like a really Kludgey way to do it.

like image 670
wdh Avatar asked Oct 05 '11 16:10

wdh


People also ask

What is raw in Twig?

raw. By default, everything in Twig gets escaped when automatic escaping is enabled. If you don't want to escape a variable you'll have to explicitly mark it as safe which you can do by using the raw filter. This only works if the raw filter is the last filter that is applied to the filter.

What is Page HTML Twig?

html. twig - the body of Developer Portal HTML page. node. html. twig - the template for all of the content nodes.


4 Answers

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.

like image 189
Haprog Avatar answered Nov 04 '22 10:11

Haprog


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 engine
AcmeBlogBundle:Blog:index.html.php - HTML format, PHP engine
AcmeBlogBundle: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.

like image 28
rdjs Avatar answered Nov 04 '22 12:11

rdjs


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.

like image 41
Chris Avatar answered Nov 04 '22 12:11

Chris


Follow up on Kari, if you're in an extension.. you can apply it this way.

public function getFunctions() {
    return [
        'include_raw' =>  new \Twig_Function_Method($this, 'twig_include_raw', array('needs_environment'=> true, 'is_safe'=> array('all')))
    ];
}

And that'd be a $this->twig_include_raw type method. You'd include within your template as:

{{ include_raw("my/file/here.html.twig") }}

No need for " | raw".

like image 45
Matt Kenefick Avatar answered Nov 04 '22 12:11

Matt Kenefick