jquery模板标签与Django模板冲突!

问题描述:

今天我试着玩jquery-tmpl {{if}} & {{else}}语句。jquery模板标签与Django模板冲突!

<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> 

但如你所知{{}}也保留为django模板。 所以Django会发出TemplateSyntaxError,它无法解析它。

我该如何解决这个问题?


更新:

我找到了一个工作<%的原始%>自定义标签(GPL)实现从这里开始。

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

使用templatetag模板标签来显示括号:

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

这是一个有点繁琐,这就是为什么raw模板标签已经提出的Django 1.3。

+0

谢谢丹尼尔!但是,如你所说,它太过于繁琐。我应该等待Django 1.3 ...:) – 2010-10-26 09:01:25

+1

或将jquery-template代码不在html中,但在代码中的某处并将其传递给模板。:( – 2010-10-26 09:11:57

+0

在我的使用案例中,客户端CMS在我的js文件中应用了django模板。 jQuery模板引擎本身的声明 – CharlesTWall3 2012-12-17 22:29:05

这里有提到的几个解决方案:

https://github.com/nje/jquery-tmpl/issues#issue/17 - 编辑:老回购

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

我最喜欢的是{% verbatim %}模板标签,它允许您从Django的模板标签中构建jQuery模板。

我使用Django 1.3,加入

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

到我的HTML文件。

服务器返回一个:

无效的块标签:“生”

而且在docs我似乎无法找到关于这个标签你谈论的信息。

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

+0

原始标签可能不包含django 1.3,所以你需要在这里包含自定义标签代码:http://www.holovaty.com/writing/django-two-phased-rendering / – 2011-07-26 07:49:43

这里有使用Django模板,使用icanhaz伟大的信息,但我认为这是类似于足以JQuery的模板。

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

我发现默认如果不支持任何“逐字”标签是有用的。 看起来来样:

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

PS。请注意ChicagoBoss的用户,该用户不支持ErlyDTL中的'verbatim'标签。