我想使用AngularJS与Django,但他们都使用{{}}作为他们的模板标签。是否有一种简单的方法来更改其中之一,以使用其他自定义模板标签?
当前回答
如果你使用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
其他回答
我投票反对使用双括号(())作为模板标签。只要不涉及函数调用,它就可以很好地工作,但当尝试以下方法时
ng:disabled=(($invalidWidgets.visible()))
在Mac上使用Firefox(10.0.2)时,我得到了一个非常长的错误,而不是预期的逻辑。<[]>对我来说很顺利,至少到目前为止。
编辑2012-03-29: 请注意,$invalidWidgets已弃用。然而,我仍然会使用另一种包装而不是双括号。对于任何高于0.10.7的angular版本(我猜),你可以在应用/模块定义中更容易地更改包装器:
angular.module('YourAppName', [], function ($interpolateProvider) {
$interpolateProvider.startSymbol('<[');
$interpolateProvider.endSymbol(']>');
});
API文档。
如果执行任何服务器端插值,唯一正确的方法是使用<>
$interpolateProvider.startSymbol('<{').endSymbol('}>');
其他任何向量都是XSS向量。
这是因为任何没有被Django转义的Angular分隔符都可以被用户输入到插入的字符串中;如果有人将他们的用户名设置为“{{evil_code}}”,Angular会很乐意运行它。但是,如果您使用的字符没有被Django转义,则不会发生这种情况。
如果你正确地分离了页面的各个部分,那么你可以很容易地在“raw”标签范围内使用angularjs标签。
在jinja2
{% raw %}
// here you can write angularjs template tags.
{% endraw %}
在Django模板中(1.5以上)
{% verbatim %}
// here you can write angularjs template tags.
{% endverbatim %}
我会坚持使用一个解决方案,既使用django标签{{}},也使用angularjs{{}},并使用一个逐字section或templatetag。
这只是因为你可以通过$interpolateProvider改变angularjs的工作方式(如上所述)。startSymbol interpolateProvider美元。但是如果你开始使用其他angularjs组件,比如ui-bootstrap,你会发现一些模板已经用标准angularjs标签{{}}构建了。
例如,查看https://github.com/angular-ui/bootstrap/blob/master/template/dialog/message.html。
如果你使用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
推荐文章
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- 在Django模型中存储电话号码的最佳方法是什么?
- jQuery UI对话框-缺少关闭图标
- 如何突出显示当前菜单项?
- 如何禁用django-rest-framework的管理风格的可浏览界面?
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 在JavaScript关联数组中动态创建键