是否有可能用角与Jinja2的模板引擎? [英] Is it possible to use Angular with the Jinja2 template engine?

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

问题描述

我有一个瓶的网站,我想使用的角度JavaScript框架。不幸的是,它好像定界符重叠。

I have a Flask site and I want to use the Angular JavaScript framework. Unfortunately, it seems as if the delimiters overlap.

我如何使用与角Jinja2的,如果两者都依赖于双花括号( {{expr}的} )?它甚至有可能?

How do I use Angular with Jinja2 if both rely on double curly braces ({{ expr }})? Is it even possible?

推荐答案

您有一些选择。

1)改变分隔符号的角度:

1) Change the delimiter notation for Angular:

var app = angular.module('Application', []);

app.config(['$interpolateProvider', function($interpolateProvider) {
  $interpolateProvider.startSymbol('{a');
  $interpolateProvider.endSymbol('a}');
}]);

不管被选择为开始和结束符号将作为新的分隔符。在这种情况下,您会前preSS一个变量采用了棱角分明 {一some_variable一个}

此方法已经被一次被明确设置只需优势。

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

由于有敏感数据的到来未膨胀从服务器端,我建议,而不是改变在其中你不是唯一的开发任何项目在前端的语法(即角)的风险较高。

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)输出中使用的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 }}' }}

这将是为输出{{} some_var} 中的HTML。

this will be output as {{ some_var }} in the HTML.

有关办法#1我的preference是显而易见的,但上述任何会工作。

My preference for approach #1 is apparent, but any of the above will work.

这篇关于是否有可能用角与Jinja2的模板引擎?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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