Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>At first I had my mustache template files named simply sometemplate.html and wrapped in <code>{% raw %}</code> 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 (<code>.mustache</code> 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.</p> <p>Here's the working Twig extension function I made:</p> <pre class="lang-php prettyprint-override"><code>function twig_include_raw(Twig_Environment $env, $template) { return $env-&gt;getLoader()-&gt;getSource($template); } $twig-&gt;addFunction('include_raw', new Twig_Function_Function('twig_include_raw', array('needs_environment' =&gt; true))); </code></pre> <p>With this in place you can easily include files as "raw" without Twig parsing them by doing:</p> <pre><code>{{ include_raw('sometemplate.mustache')|raw }} </code></pre> <p>I even made a Twig macro for simplifying including mustache templates to HTML head sections:</p> <pre class="lang-html prettyprint-override"><code>{% macro mustache_script(id, file) -%} &lt;script id="{{ id }}" type="text/x-mustache-template"&gt; {{ include_raw(file)|raw }} &lt;/script&gt; {%- endmacro %} </code></pre> <p>And after importing the file with the above macro to your Twig template (<code>{% import "macros.twig" %}</code> for example), you can easily import mustache template files in your Twig templates by simply doing <code>{{ mustache_script('sometemplate_tpl', 'sometemplate.mustache') }}</code> inside a HTML <code>&lt;head&gt;</code> section.</p> <p>I hope this helps someone who's looking for a solution to the same problem.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload