39

I need to output a portion of client-side handlebars templates, which has tags similar to twig's 'say' tags:

  <script type="text/x-handlebars">
    {{#view App.MyView}}
      <h1>Hello world!</h1>
    {{/view}}
  </script>

And twig attempts to parse these templates. How do I prevent it? Is it possible to mark a section of a template as plain text?

j0k
  • 22,600
  • 28
  • 79
  • 90
Dziamid
  • 11,225
  • 12
  • 69
  • 104

4 Answers4

47

There is raw tag for this purpose:

<script type="text/x-handlebars">
  {% raw %}
    {{#view App.MyView}}
      <h1>Hello world!</h1>
    {{/view}}
  {% endraw %}
</script>

Update

As raw tag is deprecated use verbatim instead.

Molecular Man
  • 22,277
  • 3
  • 72
  • 89
  • 2
    Better check the next answer, `{% raw %}` is deprecated to avoid confusion with the raw filter, use the tag: `{% verbatim %}...{% endverbatim %}` instead. – Rubens Mariuzzo Oct 30 '13 at 19:33
25

{% raw %} deprecated

{% verbatim %}
    <ul>
    {% for item in seq %}
        <li>{{ item }}</li>
    {% endfor %}
    </ul>
{% endverbatim %}

Source: http://twig.sensiolabs.org/doc/tags/verbatim.html

Farid Movsumov
  • 12,350
  • 8
  • 71
  • 97
1

For bigger blocks of templates I would suggest to move those script templates into a separate file/files (where I suppose they should be to make all more structured).

Then render templates in your twig by using source command {{ source('uploadables-js.html') }} (IMPORTANT, no 'use' or 'include').

0

To not litter templates with raw or verbatim tags, one can change lexar options to not conflict with client side template engines:

...
$lexer_options = [
  'tag_variable' => ['{~', '~}'],
];
$lexer = new Twig_Lexer($twig, $lexer_options);
$twig->setLexer($lexer);
Robert Brisita
  • 5,461
  • 3
  • 36
  • 35