likes
comments
collection
share

TurboGears 2介绍

作者站长头像
站长
· 阅读数 12

TurboGears 作为 Pylons 之上的全栈层诞生,现在是一个独立的 WSGI Web 框架,既可以作为全栈框架(如 Django),也可以与微框架(如 Flask)结合使用。

TurboGears 2 建立在几个下一代 Web 框架的经验之上,包括 TurboGears 1(当然)、Django 和 Rails。TurboGears 最初受到 RubyOnRails 的启发,它基于 MVC,其中控制器将请求分派给控制器本身公开的一组操作。

它最初受到 RubyOnRails 的启发,基于 MVC,控制器将请求分发给控制器本身公开的一组操作。

TurboGears 2介绍

目的:TurboGears 是一个基于 ObjectDispatch 范式的 Python Web 框架,它的目的是使在 Minimal 模式下编写小型简洁的应用程序或在 Full Stack 模式下编写复杂的应用程序成为可能。

它也是少数几个正式支持 MongoDB 作为主要存储后端的 Web 框架之一,包括支持 TurboGears Admin 从 MongoDB 模型自动生成 CRUD。

虽然 TurboGears 一直是一个全栈框架,与 Django 的项目范围相同,但它与其他框架的区别在于它对Web框架的两个主要部分的哲学:模板和路由

事实上,它依赖于一个经过验证的 XML 引擎,与 Django Template,Jinja 2和 Mako 等纯文本引擎相比,它提供了一些好处:

value='<script>alert("hello")</script>' 将呈现为

模板

虽然 TurboGears 提供了对多个模板引擎的支持,但主要的模板引擎始终是一个经过充分验证的XML模板引擎。

目前 TurboGears 附带了 Kajiki 模板引擎,该引擎是在项目本身中开发的,但在过去,它依赖于 Genshi 和 Kid 模板引擎,这些引擎大多与 Kajiki 语法兼容。

历史上经过验证的xml模板引擎总是比文本模板引擎慢,但是Kajiki项目能够创建一个非常快的模板引擎,通常比Mako或Django Template渲染得更快,同时仍然保留所有预期的功能。

自动逃脱

它会自动转义呈现到模板中的内容,从而更容易避免XSS和注入安全问题:

<div>${value}</div>

自动国际化

模板引擎解析提供的模板文档并识别包含静态文本的节点。

由于引擎能够区分文本和标记,因此它能够标记文本进行翻译。

如果提供了 "Hello World" 的翻译,像 <div>Hello World</div> 这样的内容将自动翻译,而不必在 gettext 调用中包装文本。

Compatibility with WYSIWYG Editors 兼容WYSIWYG编辑器

由于模板引擎语法是纯粹有效的 XHTML,模板本身可以用 WYSIWYG 编辑器打开,只要它们不剥离未知属性,模板就可以从这些编辑器中编辑和保存。

Routing 路由

大多数Web框架一直依赖于正则表达式来声明路由,通过装饰器或通过路由映射。

TurboGears通过 tgext.routes 扩展支持正则表达式,但首选的路由方式是通过 Object Dispatch 系统。

在Object Dispatch中,解析URL时遍历根控制器对象。url路径的每一部分都映射到控制器的一个属性(可能指向一个子控制器),直到遇到最后一个可调用的动作。

这导致了URL和服务它们的代码之间非常自然的映射,允许对项目了解最少的人跳进去并快速找到负责服务特定页面的操作。

在Object Dispatch中,像 /users/new?name=MyName 这样的URL将由像这样的对象层次结构提供:

class UsersController(TGController):
    @expose()
    def new(self, name=None):
       return 'Hi, %s' % name

class RootController(TGController):
    users = UsersController()

很容易看出 /users/new 实际上是如何解析为 RootController.users.new 的,并且提供给URL的所有选项都被传递给作为参数提供响应的操作。

TurboGears Resources TurboGears资源

  • TurboGears Introduction Video An overview of TurboGears2 features presented at the PyConWeb TurboGears简介视频在PyConWeb上展示的TurboGears 2功能概述
  • TurboGears Documentation The official TurboGears documentation TurboGears文档TurboGears官方文档
  • Microframework Mode Tutorial The official tutorial that focuses on starting TurboGears in microframework mode and leads to developement of a single file web application Microframework Mode官方教程,专注于在微框架模式下启动TurboGears,并导致开发单文件Web应用程序
  • FullStack Tutorial The Wiki in 20 minutes tutorial that showcases how to create a fully functional wiki application with TurboGears in full stack mode. FullStack 20分钟的Wiki教程,展示了如何在全栈模式下使用TurboGears创建一个功能齐全的wiki应用程序。
  • The CogBin The CogBin is a list of the most common pluggable applications for TurboGears, it enlists ready made pieces you can plug into your web application to provide features like Facebook Login, Comments, Registration and so on... CogBin CogBin是TurboGears最常见的可插拔应用程序列表,它列出了您可以插入到Web应用程序中的现成件,以提供Facebook登录,评论,注册等功能.
  • React in Pure Python An article showcasing how to create web applications relying on React without the need to have NodeJS installed at all. The article uses TurboGears as the web framework to develop the example application. React in Pure Python这篇文章展示了如何创建依赖于React的Web应用程序,而根本不需要安装NodeJS。本文使用TurboGears作为Web框架来开发示例应用程序。
  • Turbogears and the future of Python web frameworks is a Talk Python to Me podcast episode featuring an interview with one of the TurboGears core developers. TurboGears和Python Web框架的未来是一个Talk Python to Me播客节目,其中包括对TurboGears核心开发人员之一的采访。

参考链接: