Ray Yun
Ray Yun

Reputation: 1571

jquery template tags conflict with Django template!

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/

Upvotes: 21

Views: 6713

Answers (5)

Aleksandr Vinokurov
Aleksandr Vinokurov

Reputation: 306

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.

Upvotes: 0

Dantalion
Dantalion

Reputation: 343

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

Upvotes: 0

Jonas Geiregat
Jonas Geiregat

Reputation: 5442

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/

Upvotes: 0

Colin Sullivan
Colin Sullivan

Reputation: 153

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.

Upvotes: 2

Daniel Roseman
Daniel Roseman

Reputation: 599796

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.

Upvotes: 13

Related Questions