jquery template tags conflict with Django template!
Asked Answered
N

5

21

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/

Nigercongo answered 26/10, 2010 at 8:30 Comment(0)
F
13

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.

Fighter answered 26/10, 2010 at 8:35 Comment(3)
Thanks Daniel! But it's too fiddly as you said. I should wait for Django 1.3 ... :)Nigercongo
Or Put jquery-template codes not in html but somewhere in code and pass it to template.. :(Nigercongo
In my use case, the clients CMS is applying django templates in my js files. The conflicts actually go as deep as the declaration of the jQuery template engine itself.Southeasterly
E
2

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.

Ellenaellender answered 6/12, 2010 at 4:46 Comment(0)
F
0

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/

Fortunetelling answered 25/7, 2011 at 15:38 Comment(1)
Raw tag might be not included django 1.3 yet. So you need to include custom tag codes from here: holovaty.com/writing/django-two-phased-renderingNigercongo
T
0

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

Tahitian answered 24/9, 2011 at 19:27 Comment(0)
T
0

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.

Tournament answered 18/12, 2012 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.