可以将AngularJS与Jinja2模板引擎一起使用吗? [英] Is it possible to use AngularJS with the Jinja2 template engine?

查看:102
本文介绍了可以将AngularJS与Jinja2模板引擎一起使用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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_stringblock_end_stringvariable_start_stringvariable_end_stringcomment_start_stringcomment_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模板引擎一起使用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆