我想使用AngularJS与Django,但他们都使用{{}}作为他们的模板标签。是否有一种简单的方法来更改其中之一,以使用其他自定义模板标签?


当前回答

所以今天我在Angular IRC频道得到了很大的帮助。事实证明,你可以很容易地更改Angular的模板标签。以下必要的代码片段应该包含在你的angular include之后(给定的例子会出现在他们的邮件列表中,并且会使用(())作为新的模板标签,代替你自己的):

angular.markup('(())', function(text, textNode, parentElement){
  if (parentElement[0].nodeName.toLowerCase() == 'script') return;
  text = text.replace(/\(\(/g,'{{').replace(/\)\)/g, '}}');
  textNode.text(text);
  return angular.markup('{{}}').call(this, text, textNode, parentElement);
});

angular.attrMarkup('(())', function(value, name, element){
    value = value.replace(/\(\(/g,'{{').replace(/\)\)/, '}}');
    element[0].setAttribute(name, value);
    return angular.attrMarkup('{{}}').call(this, value, name, element);
});

此外,我还指出了即将进行的增强,它将公开startSymbol和endSymbol属性,可以将它们设置为您想要的任何标记。

其他回答

你可以使用{% templatetag %}标签告诉Django输出{{和}},以及其他保留的模板字符串。

例如,使用{% templatetag openvariable %}将输出{{。

你可以试试逐字逐句的Django模板标签 像这样使用它:

< script src = " https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js " > < /脚本> {% verbatim %} < div ng-app = " " > <p>10是{{5 + 5}}</p> < / div > {% endverbatim %}

如果你使用django 1.5及更新版本:

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

如果你还在使用appengine上的django 1.2,可以使用下面的命令来扩展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))

在你的文件中使用:

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

对于Angular 1.0,你应该使用$interpolateProvider api来配置插值符号:http://docs.angularjs.org/api/ng.$interpolateProvider。

像这样的东西应该可以达到目的:

myModule.config(function($interpolateProvider) {
  $interpolateProvider.startSymbol('{[{');
  $interpolateProvider.endSymbol('}]}');
});

请记住两件事:

混合服务器端和客户端模板很少是一个好主意,应该谨慎使用。主要问题是:可维护性(难以阅读)和安全性(双插值可能会暴露一个新的安全向量——例如,服务器端和客户端模板本身的转义可能是安全的,但它们的组合可能不安全)。 如果你开始使用在模板中使用{{}}的第三方指令(组件),那么你的配置将破坏它们。(修复等待)

虽然我们对第一个问题无能为力,除了警告人们,但我们确实需要解决第二个问题。

所以今天我在Angular IRC频道得到了很大的帮助。事实证明,你可以很容易地更改Angular的模板标签。以下必要的代码片段应该包含在你的angular include之后(给定的例子会出现在他们的邮件列表中,并且会使用(())作为新的模板标签,代替你自己的):

angular.markup('(())', function(text, textNode, parentElement){
  if (parentElement[0].nodeName.toLowerCase() == 'script') return;
  text = text.replace(/\(\(/g,'{{').replace(/\)\)/g, '}}');
  textNode.text(text);
  return angular.markup('{{}}').call(this, text, textNode, parentElement);
});

angular.attrMarkup('(())', function(value, name, element){
    value = value.replace(/\(\(/g,'{{').replace(/\)\)/, '}}');
    element[0].setAttribute(name, value);
    return angular.attrMarkup('{{}}').call(this, value, name, element);
});

此外,我还指出了即将进行的增强,它将公开startSymbol和endSymbol属性,可以将它们设置为您想要的任何标记。