禁用Tornadoweb中的模板处理

时间:2013-06-24 20:15:29

标签: angularjs tornado

我必须使用Tornadoweb作为我们现有AngularJs应用程序的RESTfull后端。

{{}}在角度应用中大量使用。我想将龙卷风中的角文件作为静态文件提供

有没有办法通过龙卷风禁用处理模板以避免与龙卷风使用的{{}}发生冲突?

我知道如何使用$ interpolateProvider更改角度应用中的{{}},但它会在角度应用中进行大量更改。

作为临时解决方案,我将角度应用程序放在龙卷风的静态文件夹中并使用重定向:

class IndexHandler(RequestHandler):
    def get(self):
        self.redirect(self.static_url('ng-app/index.html'),True)

它正在工作,但它不是一个很好的解决方案,因为显示的网址是:

http://localhost:8080/static/ng-app/index.html?v=efb937e6a0cb0739eb0edfd88cfb4844

有更好的主意吗?

提前谢谢

4 个答案:

答案 0 :(得分:5)

使用{{!tag}}禁用龙卷风的翻译。

答案 1 :(得分:4)

您可以使用Tornado内置的StaticFileHandler。这会将所有内容作为静态文件传递给Angular:

from tornado import options, ioloop, web

options.define("port", default=8888, help="run on the given port", type=int)

SETTINGS = {
    "debug" : True
}

application = web.Application([
    (r'/(.*)', web.StaticFileHandler, {"path": "angular_app"})
],**SETTINGS)


if __name__ == "__main__":
    options.parse_command_line()
    application.listen(options.options.port)
    ioloop.IOLoop.instance().start()

答案 2 :(得分:2)

棘手且非常丑陋的解决方案:您可以使用self.write()代替self.render()来打印文件内容。如果它是一个HTML页面,那么对.css,.js文件和图像会有更多的GET请求,所以你必须有第二个处理程序来返回它们。 AngularJS应用程序的示例来自:http://architects.dzone.com/articles/angularjs-get-first-impression

项目树:

$ tree
.
├── angular_app
│   ├── css
│   │   ├── app.css
│   │   └── bootstrap.css
│   ├── img
│   │   └── ajax-loader.gif
│   ├── index.html
│   └── js
│       ├── app.js
│       ├── contollers
│       │   └── CurrencyConvertCtrl.js
│       ├── db.js
│       ├── models
│       │   └── Currency.js
│       ├── _references.js
│       └── vendor
│           ├── angular.js
│           ├── bootstrap.js
│           ├── highcharts.js
│           └── jquery-1.9.1.js
├── test.py
└── test.py~

龙卷风代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web

import logging

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

import os 
angular_app_path=os.path.join(os.path.dirname(__file__), "angular_app")

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        with open(angular_app_path + "/index.html", 'r') as file:
            self.write(file.read())     

class StaticHandler(tornado.web.RequestHandler):
    def get(self): 
        self.set_header('Content-Type', '') # I have to set this header
        with open(angular_app_path + self.request.uri, 'r') as file:
            self.write(file.read())

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(
    handlers=[(r'/', IndexHandler), (r'/js.*', StaticHandler), (r'/cs.*', StaticHandler), (r'/img.*', StaticHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

答案 3 :(得分:-1)

愚蠢的方式:

只需使用:

self.render('some-template.html', var1='{{var1}}') 

{{var1}}后,模板首先删除{{ }},然后替换var1,以便在呈现的html文件中结果为{{var1}};)

相关问题