Green
Green

Reputation: 30805

How to use Mustache in Twig templates? They both have the same tag delimiters

I use Twig in my project. It uses these tags: {{ name }}

I want to include Mustache in my project as well. But Mustache also uses the same tags {{ name }}, so there is a conflict and nothing works.

The solution Mustache provides in their docs of course doesn't work. Because I have to type Twig delimiter {{ in my Twig template to change Mustache delimiter.

* {{ default_tags }}
{{=<% %>=}}
* <% erb_style_tags %>
<%={{ }}=%>
* {{ default_tags_again }} 

How to pass over it? Is there any other way to change Mustache delimiters once somewhere in settings?

Upvotes: 3

Views: 3346

Answers (4)

Dung
Dung

Reputation: 20565

'{% verbatim %}
<a href="{{joblink}}" target="_blank">{{{_highlightResult.title.value}}}</a><p>{{description}}</p>
{% endverbatim %}'

Explanation:

  • {% verbatim %} tells twig not interpret {{some content}} as twig

  • html syntax remains the same such as link

  • so if your code/function/config expect Mustache string, there you have it: {{Mustache.object.value}}.

From source:

https://github.com/janl/mustache.js/issues/441

Tested working in Symfony framework.

Upvotes: 0

Veve
Veve

Reputation: 6758

You can customize twig delimiters to be differents than Mustache ones by replacing {{ and }} by [[ and ]] for example, as explained in Twig doc:

$twig = new Twig_Environment();

$lexer = new Twig_Lexer($twig, array(
    'tag_comment'   => array('{#', '#}'),
    'tag_block'     => array('{%', '%}'),
    'tag_variable'  => array('[[', ']]'),
    'interpolation' => array('#{', '}'),
));
$twig->setLexer($lexer);

Upvotes: 2

user6306158
user6306158

Reputation: 51

If you have a large template to pass through twig. A better solution is to temporarily turn off autoescape.

{% verbatim %}
    Everything will be outputted as is in this block
{% endverbatim %}

Upvotes: 5

po_taka
po_taka

Reputation: 1856

Workaround is to "echo" double brackets with twig

{{ "{{some_js_varaible}}" }}

and

{{ "{% js_condition %}" }}

Upvotes: 1

Related Questions