21
votes

Today I'm trying to play with jquery-tmpl {{if}} & {{else}} statements.

<script id="mission-dialog" type="text/x-jquery-tmpl">
    <h3>${name}</h3>
    <p>${description}</p>
    <ul>
        {{each(i,cond) conditions.data}}
        <li>
            <img src="${cond.image}"/>
            <h4>${cond.name}</h4>
            <p class="status">${cond.status.value}/${cond.status.max}</p>
        </li>
        {{/each}}
    </ul>
</script>

But as you know {{ }} is reserved also for django template. So django will emit TemplateSyntaxError that it can't parse it.

How can I solve this problem?


updated:

I found a working <% raw %> custom tag (GPL) implementation from here.

http://www.holovaty.com/writing/django-two-phased-rendering/

5

5 Answers

13
votes

Use the templatetag template tag to render the brackets:

{% templatetag openvariable %}each(i,cond) conditions.data{% templatetag closevariable %}

It's a bit fiddly, which is why a raw template tag has been proposed for Django 1.3.

2
votes

There are a few solutions mentioned here:

https://github.com/nje/jquery-tmpl/issues#issue/17 - Edit: Old repo

https://github.com/jquery/jquery-tmpl/issues/#issue/74

My favorite is the {% verbatim %} template tag that allows you to build jQuery templates from within Django ones.

0
votes

I'm using Django 1.3 and adding

{% raw %} this should be ignored by django's template framework {% endraw %}

to my html file.

The server returns with a:

Invalid block tag: 'raw'

Also in the docs I can't seem to locate information about this tag you're talking about.

https://docs.djangoproject.com/en/1.3/ref/templates/builtins/

0
votes

There's great information here on using Django templates, using icanhaz, but I think this is similar enough to JQuery templates.

http://tothinkornottothink.com/post/4282971041/using-jquery-templating-icanhaz-js-with-django

0
votes

I've found defaults to be useful if no 'verbatim' tag is supported. Looks like this:

{{ some_undefined_variable|default: '....here goes the subject template....'}}

PS. Note for users of ChicagoBoss, which has no support for 'verbatim' tag in ErlyDTL.