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 ResourcesTurboGears资源

  • TurboGears Introduction VideoAn overview of TurboGears2 features presented at the PyConWeb
    TurboGears简介视频在PyConWeb上展现的TurboGears 2功用概述
  • TurboGears DocumentationThe official TurboGears documentation
    TurboGears文档TurboGears官方文档
  • Microframework Mode TutorialThe 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 TutorialThe 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 CogBinThe 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 PythonAn 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 frameworksis 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中心开发人员之一的采访。

参考链接: