我有一个Flask网站,我想使用AngularJS JavaScript框架.不幸的是,似乎分隔符重叠.
I have a Flask site and I want to use the AngularJS JavaScript framework. Unfortunately, it seems as if the delimiters overlap.
如果两个都依赖双花括号({{ expr }}),如何将Angular与Jinja2一起使用?甚至有可能吗?
How do I use Angular with Jinja2 if both rely on double curly braces ({{ expr }})? Is it even possible?
推荐答案您有一些选择.
1)更改Angular的定界符表示法:
1) Change the delimiter notation for Angular:
var app = angular.module('Application', []); app.config(['$interpolateProvider', function($interpolateProvider) { $interpolateProvider.startSymbol('{a'); $interpolateProvider.endSymbol('a}'); }]);无论为开始和结束符号选择了什么,都将用作新的定界符.在这种情况下,您可以使用{a some_variable a}将变量表示为Angular.
Whatever is chosen for the start and end symbols will act as the new delimiters. In this case, you would express a variable to Angular using {a some_variable a}.
这种方法的优点是只需要设置一次就可以了.
This approach has the advantage of only needed to be set once and being explicit.
2)更改Jinja2的定界符表示法.
2) Change the delimiter notation for Jinja2.
覆盖或子类Flask.jinja_options.update(相关变量:block_start_string,block_end_string,variable_start_string,variable_end_string,comment_start_string,comment_end_string):
Override or subclass Flask.jinja_options.update on the Flask object that you bind to your application (relevant vars: block_start_string, block_end_string, variable_start_string, variable_end_string, comment_start_string, comment_end_string):
jinja_options = app.jinja_options.copy() jinja_options.update(dict( block_start_string='<%', block_end_string='%>', variable_start_string='%%', variable_end_string='%%', comment_start_string='<#', comment_end_string='#>' )) app.jinja_options = jinja_options由于敏感数据从服务器端未展开的风险较高,因此我建议在您不是唯一开发人员的任何项目中更改前端(即Angular)的语法.
As there's a higher risk of sensitive data coming un-expanded from from the server-side, I suggest instead changing the syntax on the front-end (i.e. Angular) on any project in which you're not the sole developer.
3)输出一个 使用{% raw %}或{% verbatim %}在Jinja2中原始块:
3) Output a raw block in Jinja2 using {% raw %} or {% verbatim %}:
<ul> {% raw %} {% for item in seq %} <li>{{ some_var }}</li> {% endfor %} {% endraw %} </ul>4)使用Jinja2在模板中写花括号:
4) Use Jinja2 to write the curly braces in the template:
{{ '{{ some_var }}' }}这将在HTML中作为{{ some_var }}输出.
this will be output as {{ some_var }} in the HTML.
我对方法1的偏好是显而易见的,但是以上任何一种方法都可以.
My preference for approach #1 is apparent, but any of the above will work.
更多推荐
可以将AngularJS与Jinja2模板引擎一起使用吗?
发布评论