Marco
Marco

Reputation: 789

Mustache and Twig: not escaping HTML

I've got Twig and Mustache combined. Since they both use same tags for bookmarks

{{ var }}

I changed Mustache tags, like this

Mustache.parse(template, ['${', '}']);

So I use this bookmark in Mustache

${ var }

And there are no conflicts. The problem comes out when I need to tell Mustache not to escape HTML. As the Documentation says, I am to use

{{{ do_not_escape_this_var }}}

But this leads to a conflict with Twig! And It seems like there is no way tu customize it.

I know I could override Twig's bookmarks, but this is bad for the frontend team, used to Twig default tag.

Any solutions? Thank you

Upvotes: 1

Views: 528

Answers (1)

Matias Kinnunen
Matias Kinnunen

Reputation: 8540

Like the Twig's documentation says, code can be escaped like this:

{{ '{{{ do_not_escape_this_var }}}' }}

For larger blocks of code, you can use verbatim to prevent Twig from parsing Mustache code as Twig code:

{% verbatim %}
    {{{ do_not_escape_this_var }}}
{% endverbatim %}

With verbatim, you could also avoid parsing chunks of Mustache code with the default Mustache tags:

{% verbatim %}
    <h1>Hello {{ name }}!</h1>
    <p>How are you {{ today }}?</p>
    {{! This is just Mustache, not Twig }}
{% endverbatim %}

Of course, then you can't use Twig between {% verbatim %} and {% endverbatim %}. I don't think it's a problem as mixing Mustache (with the default tags) and Twig code would be very confusing.

Upvotes: 2

Related Questions