Michaël Perrin
Michaël Perrin

Reputation: 6268

How to escape Twig delimiters in a Twig template?

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.

Upvotes: 51

Views: 30475

Answers (3)

raina77ow
raina77ow

Reputation: 106483

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.

Upvotes: 90

Neha Patel
Neha Patel

Reputation: 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 %} 

Upvotes: -2

lonesomeday
lonesomeday

Reputation: 238115

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 %}

Upvotes: 16

Related Questions