Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja and javascript syntax conflict?

I am using jinja2 templating language in my GAE-python project. I have tried to use the jquery-upload for uploading files.

The following code is throwing an error:

<!-- The template to display files available for upload -->¬
    152 <script id="template-upload" type="text/x-tmpl">¬
--  153     {% for (var i=0, file; file=o.files[i]; i++) { %}¬
|   154     <tr class="template-upload fade">¬
|   155         <td class="preview"><span class="fade"></span></td>¬
|-  156         <td class="name"><span>{%=file.name%}</span></td>¬

The above code is directly taken from the jquery upload library.

The error:

line 153, in template
    {% for (var i=0, file; file=o.files[i]; i++) { %}
TemplateSyntaxError: expected token ')', got 'i'

I think it is being caused due to the {% %} which is used by jinja2 as well as the text/x-tmpl js syntax. Is this correct? If so, How can I work around it? Please help.

like image 358
abhinav Avatar asked Aug 23 '12 13:08

abhinav


1 Answers

Try this:

{{ '{% for (var i=0, file; file=o.files[i]; i++) { %}' }}

Or you can use {% raw %} {% endraw %} blocks.

http://jinja.pocoo.org/docs/templates/#escaping

like image 194
djc Avatar answered Oct 22 '22 09:10

djc