码迷,mamicode.com
首页 > 其他好文 > 详细

解决jinja2和angular的花括号{{}}冲突的方法。

时间:2015-02-17 13:00:13      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

一共3个方法,

A、http://flask-triangle.readthedocs.org/en/develop/tutorial/part1.html

上代码

app.py

from flask import Flask, render_template
from flask.ext.triangle import Triangle

    app = Flask(__name__, static_path=‘/static‘)
    Triangle(app)

    @app.route(‘/‘)def index():
        return render_template(‘index.html‘)if __name__ == ‘__main__‘:
        app.run()

templates/index.html:

<!DOCTYPE html>
 <html data-ng-app>
 <head>
 <meta charset="utf-8">
 <script src="/static/js/angular.min.js"></script>
 <title>Flask-Triangle - Tutorial</title>
 </head>
 <body>
 <label>Name:</label>
 <input type="text" data-ng-model="yourName" placeholder="Enter a name here">
 <hr>
 <h1>Hello {{yourName|angular}}!</h1>
 </body>
 </html>


B、C都是这里的:http://lorenhoward.com/blog/how-to-get-angular-to-work-with-jinja/

B、通过 verbatim 来暂停jinja2的解析

{% raw %}
<h1 class="user-name">{{ user.name }}</h1>
{% endraw %}

C、修改angular的符号

On the front end, after instantiating our Angular app, we can tell Angular to look for different binding tags (instead of ‘{{‘ and ‘}}‘). 

var app = angular.module(‘myApp‘, []);

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

The above code snippet tells angular to look for ‘{[‘ as a an opening tag, and ‘]}‘ for a closing tag.

Now we can use both Angular and Jinja together at the same time:

<h1 class="{{ some_class }}">{[ foo.bar ]}</h1>

As one can see, the h1 element‘s class will be rendered in the backend via Jinja and Flask / Django.  When it‘s sent to the browser, it might look like this:

<h1 class="some-class">{[ foo.bar ]}</h1>

From there, Angular will see that ‘{[ foo.bar ]}‘ should be a binding and will update the view accordingly.


我的项目用angular比较少,所以我选择了B。

解决jinja2和angular的花括号{{}}冲突的方法。

标签:

原文地址:http://my.oschina.net/klausgao/blog/379273

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!