Search code examples
twig

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.


Solution

  • 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.