可以将AngularJS与Jinja2模板引擎一起使用吗?

编程入门 行业动态 更新时间:2024-10-25 06:22:26
本文介绍了可以将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_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模板引擎一起使用吗?

本文发布于:2023-11-24 05:36:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624124.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   引擎   AngularJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!