Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig: prevent parsing of client-side templates

Tags:

twig

symfony

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?

like image 616
Dziamid Avatar asked Feb 25 '12 10:02

Dziamid


People also ask

Can twig ignore parts it would otherwise handle as variables?

It is sometimes desirable or even necessary to have Twig ignore parts it would otherwise handle as variables or blocks. For example if the default syntax is used and you want to use { { as raw string in the template and not start a variable you have to use a trick.

How do I execute a Twig template from a browser?

Netbeans via the Twig syntax plugin (until 7.1, native as of 7.2) PhpStorm (native as of 2.1) Also, TwigFiddle is an online service that allows you to execute Twig templates from a browser; it supports all versions of Twig. The application passes variables to the templates for manipulation in the template.

What is the result of evaluating that expression in twig?

The result of evaluating that expression is inserted into the string: Tag level line whitespace control was added in Twig 2.8. The first newline after a template tag is removed automatically (like in PHP). Whitespace is not further modified by the template engine, so each whitespace (spaces, tabs, newlines etc.) is returned unchanged.

What is twigfiddle and how to use it?

Also, TwigFiddle is an online service that allows you to execute Twig templates from a browser; it supports all versions of Twig. The application passes variables to the templates for manipulation in the template. Variables may have attributes or elements you can access, too.


2 Answers

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.

like image 125
Molecular Man Avatar answered Oct 07 '22 03:10

Molecular Man


{% raw %} deprecated

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

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

like image 29
Farid Movsumov Avatar answered Oct 07 '22 03:10

Farid Movsumov