Twig: evitar el análisis de plantillas del lado del cliente


Necesito generar una porción de plantillas de manillares del lado del cliente, que tiene etiquetas similares a las etiquetas 'say' de twig:

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

Y twig intentan analizar estas plantillas. ¿Cómo lo prevengo? ¿Es posible marcar una sección de una plantilla como texto sin formato?

Author: j0k, 2012-02-25

4 answers

Existe la etiqueta raw para este propósito:

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

Actualizar

Como la etiqueta raw está obsoleta use verbatim en su lugar.

 44
Author: Molecular Man,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-18 11:28:39

{%raw %} obsoleto

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

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

 25
Author: Farid Movsumov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-11 14:18:47

Para bloques más grandes de plantillas, sugeriría mover esas plantillas de script a un archivo/archivos separados (donde supongo que deberían estar para que todo sea más estructurado).

Luego renderiza las plantillas en tu twig usando el comando source {{ source('uploadables-js.html')}} (IMPORTANTE, no ' use ' o 'include').

 0
Author: Max Kapshtyk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-10-27 11:07:43

Para no ensuciar plantillas con etiquetas raw o verbatim, uno puede cambiar lexar opciones para no entrar en conflicto con los motores de plantillas del lado del cliente:

...
$lexer_options = [
  'tag_variable' => ['{~', '~}'],
];
$lexer = new Twig_Lexer($twig, $lexer_options);
$twig->setLexer($lexer);
 0
Author: Robert Brisita,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-27 20:27:10