AngularJS with Django - Conflicting template tags

前端 未结 12 2153
南笙
南笙 2020-11-22 13:39

I want to use AngularJS with Django however they both use {{ }} as their template tags. Is there an easy way to change one of the two to use some other custom

12条回答
  •  花落未央
    2020-11-22 14:06

    If you use django 1.5 and newer use:

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

    If you are stuck with django 1.2 on appengine extend the django syntax with the verbatim template command like this ...

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

    In your file use:

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

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

提交回复
热议问题