50

Twig uses the {{ }}, {% %}, {# #} delimiters.

But how can I display {{ }} in a Twig template? I'm not talking about HTML escaping.

I ask the question as I want to include a mustache template in my Twig template so that I will fill with data coming from an AJAX call.

0

3 Answers 3

86

The easiest way is to output the variable delimiter ({{) by using a variable expression:

{{ '{{' }}

Alternatives (used when you have to escape too much) are raw (verbatim since 1.12) blocks:

{% raw %}
    <ul>
    {% for item in seq %}
        <li>{{ item }}</li>
    {% endfor %}
    </ul>
{% endraw %}

Actually, it's quite well documented.

5
  • Thank you, I looked for it in the documentation but didn't see it. The raw is the perfect solution to this problem. Sep 19, 2012 at 14:50
  • 6
    the raw tag has now been deprecated in favor of the 'verbatim' tag
    – DMTintner
    Oct 31, 2013 at 15:23
  • 1
    {{ '{{' }} foo {{ '}}' }} using twig to render twig to render twig to render ....
    – Petah
    Oct 2, 2014 at 1:58
  • 2
    In Twig2, the tags are now {{ verbatim }} and {{ endverbatim }}
    – alttag
    Nov 8, 2018 at 16:20
  • 4
    No, the tags are actually {% verbatim %} and {% endverbatim %} Dec 3, 2019 at 16:55
16

The Twig documentation gives two suggestions. The first is simply to output a string:

{{ '{{' }}

Otherwise, if you want to output a long section (it sounds like you do) you can use the raw tag:

{% raw %}
    your mustache content here
{% endraw %}
1
  • raw is now verbatim. This changed in Twig version 1.12 (as of writing, 3.x is common). So: {% verbatim %}your mustache content here{% endverbatim %}
    – ianp
    Aug 14, 2020 at 15:25
-1
{% block body %} 
<table class="table table-striped"> 
    {% for temp in data %} 
    <tr> 
        <td>{{temp.fname}}</td> 
        <td>{{temp.lname}}</td> 
    </tr> 
    {% endfor %}
</table>
{% endblock %} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.