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

為{幸葍}努か 提交于 2020-01-04 15:27:42

问题


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?


回答1:


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



回答2:


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);



回答3:


Workaround is to "echo" double brackets with twig

{{ "{{some_js_varaible}}" }}

and

{{ "{% js_condition %}" }}



回答4:


'{% 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.



来源:https://stackoverflow.com/questions/29509827/how-to-use-mustache-in-twig-templates-they-both-have-the-same-tag-delimiters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!