Ho alcuni file html che includono modelli da utilizzare da jQuery.tmpl. Alcuni tag tmpl (come {{if...}}
) assomigliano ai tag modello Django e causano un'eccezione TemplateSyntaxError. C'è un modo per specificare che il sistema di template Django dovrebbe ignorare alcune righe e produrle esattamente così come sono?Come posso comunicare ai modelli di Django di non analizzare un blocco contenente codice simile ai tag del modello?
risposta
Il modo predefinito sarebbe quello di sfuggire manualmente ad ogni modello con il tag modello templatetag
(https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#templatetag), ma sospetto che non sia quello che si vuole fare.
Ciò che si desidera veramente è un modo per contrassegnare un intero blocco come testo non elaborato, che richiede un nuovo tag personalizzato. Si potrebbe voler controllare il tag raw
qui: http://www.holovaty.com/writing/django-two-phased-rendering/
EDIT: Come di Django 1.5, questo è ora gestito dal built-in tag verbatim
modello
Ci sono un paio biglietto aperto per affrontare questo problema: https://code.djangoproject.com/ticket/14502 e https://code.djangoproject.com/ticket/16318 è possibile trovare una proposta di un nuovo modello di tag verbatim
di seguito:
"""
From https://gist.github.com/1313862
"""
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))
penso che il tag 'raw' è una soluzione più elegante di questi. Inoltre 'verbatim' non gestisce tag di commento e restituisce' noparse' e stringa vuota. – Jake
Se ti senti in questo modo dovresti essere sicuro di commentare i biglietti pertinenti. È la community che decide quali caratteristiche stanno andando in Django. Non sto dicendo che questo è il modo migliore per farlo, ma questo è ciò che la comunità sta attualmente muovendo verso. –
A un'osservazione più ravvicinata, è chiaro che noparse attraversa i token nel blocco e li imposta tutti su token di testo. – Jake