0
votes

I have a .twig file with some content that is rendered using backbone via underscore's template engine.

index.twig file

<select name="" id="" class='select-specializations'>
    <% _.each(itemList, function(item){%>
    <option value="<%= item %>"> <%= item %> </option>
    <%})%>
</select>

The problem is that twig doesn't seem to ignore this <% syntax when I autoescape in my template file. Throwing the following error:

A block must start with a tag name in..

And if I use the raw block, underscore doesn't seem to undestand the syntax. Is there a was to resolve this conflict of syntaxes between twig and underscore?

2
If you want Underscore to parse it, why have it as a twig template? - Jonathan Lonowski
@JonathanLonowski The html page is generated using symphony. - tusharmath
@CarrieKendall How is that question relevant to mine? - tusharmath

2 Answers

0
votes

I faced same issue as Andrew mentioned. The solution is to escape the underscore template tags. Below is the final code after escaping.

<select name="" id="" class='select-specializations'>
    {{ "<% _.each(itemList, function(item){%>" }}
    <option value="{{ "<%= item %>" }}"> {{ "<%= item %>" }} </option>
    {{ "<%})%>" }}
</select>

You will find more info from Twig documentation

-1
votes

Surround your underscore code with verbatim. See http://twig.sensiolabs.org/doc/tags/verbatim.html for details.

Side note: <% is not the problem, but {% is.