24

相同的語法我嘗試走私HTML模板中的HTML mustache.js,但是 Django模板引擎刪除,應該是 輸出的所有佔位符,是對前端Django和小鬍子使用模板

的模板包括在HTML中這樣說:

<script type="text/x-mustache-template" data-id="header_user_info"> 
    <div id="header_user_info"> 
     <div id="notification">0</div> 
     <a href="#">{{username}}</a> 
    </div> 
</script> 

,我可以通過運行$(EL)的.html()獲得HTML模板,並利用Mustache.to_html(臨時生成的HTML ,數據);

我可以把所有的模板放到另一個靜態文件中,並從 CDN中提供服務,但是很難追蹤模板所屬的地方, 和至少一個額外的http請求。

+0

@Alasdair見我的回答一個更好的方式來做到這一點。 'templatetag'解決方案太冗長了。 – surjikal

+0

您可以在項目中使用逐字標記。看看[這個鏈接](https://code.djangoproject.com/ticket/16318)。 –

回答

21

您可以使用{% templatetag %} templatetag打印出通常由Django處理的字符。例如:

{% templatetag openvariable %} variable {% templatetag closevariable %} 

結果在你的HTML如下:

{{ variable }} 

有關參數的完整列表,請參閱:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#templatetag

+0

謝謝。這個對我有用。 –

+1

太好了。然後接受我的答案。你也應該接受你提出的其他問題的答案。 –

+0

不錯的一個!不知道這個標籤。 –

6

嘗試使用django-mustachejs

{% load mustachejs %} 
{% mustachejs "main" %} 

的Django -mustachejs將生成以下內容:

<script>Mustache.TEMPLATES=Mustache.TEMPLATES||{};Mustache.TEMPLATES['main']='<<Your template >>';</script> 
2

我有同樣的問題,但使用

{% templatetag openvariable %} variable {% templatetag closevariable %} 

對我來說太冗長。我只是增加了一個非常簡單的自定義模板標籤:

@register.simple_tag 
def mtag(tagContent): 
    return "{{%s}}" % tagContent 

所以,我現在可以這樣寫:

{% mtag "variable" %} 
1

我有同樣的問題,所以大部分時間我的變量的一部分可翻譯的字符串。

{% trans "The ball is {{ color }}" %} 

即使不提供i18n,也可以使用反式模板標籤。

47

您可以簡單地改變標籤:

Mustache.tags = ['[[', ']]']; 
+6

問題的最佳途徑,謝謝! – FRD

+0

更改Mustache標籤以處理Django問題?不理想。過去沒有太多選擇,但是Django 1.5現在支持逐字標記。請參閱下面的@cat回答。 – mynameistechno

+1

爲什麼逐字標記更好? – surjikal

20

如果你使用Django 1.5和較新的用途:

{% verbatim %} 
    {{if dying}}Still alive.{{/if}} 
    {% endverbatim %} 

如果你被卡住的Django 1.2 AppEngine上與逐字延長Django的語法模板命令是這樣的...

from django import template 

register = template.Library() 

class VerbatimNode(template.Node): 

    def __init__(self, text): 
     self.text = text 

    def render(self, context): 
     return self.text 

@register.tag 
def verbatim(parser, token): 
    text = [] 
    while 1: 
     token = parser.tokens.pop(0) 
     if token.contents == 'endverbatim': 
      break 
     if token.token_type == template.TOKEN_VAR: 
      text.append('{{') 
     elif token.token_type == template.TOKEN_BLOCK: 
      text.append('{%') 
     text.append(token.contents) 
     if token.token_type == template.TOKEN_VAR: 
      text.append('}}') 
     elif token.token_type == template.TOKEN_BLOCK: 
      text.append('%}') 
    return VerbatimNode(''.join(text)) 

在你的文件(python 2。7,HDR)使用方法:

from django.template import Context, Template 
import django 
django.template.add_to_builtins('utilities.verbatim_template_tag') 

html = Template(blob).render(Context(kwdict)) 

在您的文件(Python的2.5)使用方法:

from google.appengine.ext.webapp import template 
template.register_template_library('utilities.verbatim_template_tag') 

來源: http://bamboobig.blogspot.co.at/2011/09/notebook-using-jquery-templates-in.html

1

您可以使用內置的mustache.js設置分隔符標籤更改鬍鬚使用的默認標籤。

{{=<% %>=}} 

現在你可以這樣做:

<% variable %>