|
| 1 | +title: TurboGears |
| 2 | +category: page |
| 3 | +slug: turbogears |
| 4 | +sortorder: 0405 |
| 5 | +toc: False |
| 6 | +sidebartitle: TurboGears |
| 7 | +meta: TurboGears is a batteries included web framework that can act both as a full stack or microframework solution, with MongoDB as a first citizen storage. |
| 8 | + |
| 9 | +# TurboGears |
| 10 | +[TurboGears](http://www.turbogears.org), born as a full stack layer on top |
| 11 | +of Pylons, is now a standalone [WSGI](/wsgi-servers.html) web framework |
| 12 | +that can act both as a full stack framework (like [Django](/django.html)) |
| 13 | +or as a micro framework (like [Flask](/flask.html)) |
| 14 | + |
| 15 | +<a href="http://www.turbogears.org/" style="border: none;"> |
| 16 | + <img src="/img/logos/turbogears.jpg" width="100%" alt="TurboGears logo." class="technical-diagram" style="border-radius: 5px;"> |
| 17 | +</a> |
| 18 | + |
| 19 | +Originally inspired by RubyOnRails it's based on MVC |
| 20 | +where the controller dispatches the request to a set of actions |
| 21 | +exposed from the controller itself. |
| 22 | + |
| 23 | +TurboGears, in its full stack mode, provides all the features |
| 24 | +you would require during development of a web application: |
| 25 | + |
| 26 | +* [Identification and Authentication](http://turbogears.readthedocs.io/en/latest/turbogears/authentication.html) |
| 27 | +* [Authorization](http://turbogears.readthedocs.io/en/latest/turbogears/authorization.html) |
| 28 | +* [Autogenerated Admin and CRUD](http://turbogears.readthedocs.io/en/latest/cookbook/admin.html) |
| 29 | +* [Sessions](http://turbogears.readthedocs.io/en/latest/turbogears/session.html) |
| 30 | +* [Caching](http://turbogears.readthedocs.io/en/latest/turbogears/caching.html) |
| 31 | +* [Schema Migrations](http://turbogears.readthedocs.io/en/latest/turbogears/migrations.html) |
| 32 | +* [Master/Slave Database Queries Balancing](http://turbogears.readthedocs.io/en/latest/cookbook/master-slave.html) |
| 33 | +* Request Bound Transactions |
| 34 | +* Interactive Debugger |
| 35 | +* Builtin Profiling |
| 36 | +* Pluggable Applications |
| 37 | + |
| 38 | +It's also one of the few web frameworks officially supporting |
| 39 | +MongoDB as one of the primary storage backends, including |
| 40 | +support into the TurboGears Admin to autogenerate CRUDs |
| 41 | +from MongoDB models. |
| 42 | + |
| 43 | +While TurboGears has always been a full stack framework |
| 44 | +with same scope of projects like Django, it differentiates |
| 45 | +from other frameworks due the its philosophy on two major |
| 46 | +parts of a web framework: *Templating* and *Routing* |
| 47 | + |
| 48 | +## Templating |
| 49 | + |
| 50 | +While TurboGears provides support for multiple template |
| 51 | +engines, the primary one has always been a fully |
| 52 | +validated XML template engine. |
| 53 | + |
| 54 | +Currently TurboGears ships with the ``Kajiki`` template engine, |
| 55 | +which was developed within the project itself, but in the past |
| 56 | +it relied on the Genshi and Kid template engines which were |
| 57 | +mostly syntax compatible with Kajiki. |
| 58 | + |
| 59 | +Historically validated xml template engines has always been |
| 60 | +slower than text template engines, but the Kajiki project |
| 61 | +was able to create a very fast template engine that usually |
| 62 | +renders faster than Mako or Django Template while |
| 63 | +still retaining all the expected features. |
| 64 | + |
| 65 | +The fact that it relies on a validated XML engine provides |
| 66 | +some benefits compared to plain text engines like Django |
| 67 | +Template, Jinja2 and Mako: |
| 68 | + |
| 69 | +### Automatic Escaping |
| 70 | + |
| 71 | +It automatically escapes content rendered into the |
| 72 | +template, thus making easier to avoid XSS and injection |
| 73 | +security issues: |
| 74 | + |
| 75 | +``` |
| 76 | +<div>${value}</div> |
| 77 | +``` |
| 78 | + |
| 79 | +with ``value='<script>alert("hello")</script>'`` |
| 80 | +will render as |
| 81 | + |
| 82 | +``` |
| 83 | +<div><script>alert("hello")</script></div>; |
| 84 | +``` |
| 85 | + |
| 86 | +thus preventing any form of injection from user provided content. |
| 87 | + |
| 88 | +### Automatic Internationalization |
| 89 | + |
| 90 | +The template engine parses the provided template document |
| 91 | +and recognises the nodes that contain static text. |
| 92 | + |
| 93 | +As the engine is able to distinguish text from markup it's |
| 94 | +able to flag the text for translation. |
| 95 | + |
| 96 | +Content like ``<div>Hello World</div>`` would get automatically |
| 97 | +translated if a translation for ``"Hello World"`` is provided, |
| 98 | +without having to wrap text in ``gettext`` calls. |
| 99 | + |
| 100 | +### Compatibility with WYSIWYG Editors |
| 101 | + |
| 102 | +As the template engine syntax is purely valid XHTML |
| 103 | +the template itself can be opened with WYSIWYG editors |
| 104 | +and as far as they don't strip unknown attributes |
| 105 | +the template can be edited and saved back from those editors. |
| 106 | + |
| 107 | +## Routing |
| 108 | + |
| 109 | +Most web frameworks have been relying on regular expressions |
| 110 | +to declare routing, through decorators or through a routing map. |
| 111 | + |
| 112 | +TurboGears supports regular expressions through the ``tgext.routes`` |
| 113 | +extension, but the preferred way of routing is through the |
| 114 | +``Object Dispatch`` system. |
| 115 | + |
| 116 | +In Object Dispatch a root controller object is traversed while |
| 117 | +resolving the URL. Each part of the url path is mapped to a property |
| 118 | +of the controller (Which might point to a sub controller) until |
| 119 | +a final collable action is encountered. |
| 120 | + |
| 121 | +This leads to a very natural mapping between URLs and the code |
| 122 | +serving them, allowing people with minimal knowledge of a project |
| 123 | +to jump in and quickly find actions in charge of serving a specific page. |
| 124 | + |
| 125 | +In Object Dispatch an URL like ``/users/new?name=MyName`` |
| 126 | +would be served by a hierarchy of objects like: |
| 127 | + |
| 128 | +``` |
| 129 | +class UsersController(TGController): |
| 130 | + @expose() |
| 131 | + def new(self, name=None): |
| 132 | + return 'Hi, %s' % name |
| 133 | +
|
| 134 | +class RootController(TGController): |
| 135 | + users = UsersController() |
| 136 | +``` |
| 137 | + |
| 138 | +It's easy to see how ``/users/new`` actually resolves |
| 139 | +to ``RootController.users.new`` and all options provided |
| 140 | +to the URL are passed to the action serving the respose |
| 141 | +as arguments. |
| 142 | + |
| 143 | +## TurboGears Resources |
| 144 | + |
| 145 | +* [TurboGears Introduction Video](https://www.youtube.com/watch?v=-QqQVBzU4lM&t=16s) |
| 146 | + An overview of TurboGears2 features presented at the PyConWeb |
| 147 | + |
| 148 | +* [TurboGears Documentation](http://turbogears.readthedocs.io/en/latest/) |
| 149 | + The official TurboGears documentation |
| 150 | + |
| 151 | +* [Microframework Mode Tutorial](http://turbogears.readthedocs.io/en/latest/turbogears/minimal/index.html) |
| 152 | + The official tutorial that focuses on starting TurboGears in microframework |
| 153 | + mode and leads to developement of a single file web application |
| 154 | + |
| 155 | +* [FullStack Tutorial](http://turbogears.readthedocs.io/en/latest/turbogears/wiki20.html) |
| 156 | + The Wiki in 20 minutes tutorial that showcases how to create a fully |
| 157 | + functional wiki application with TurboGears in full stack mode. |
| 158 | + |
| 159 | +* [The CogBin](http://www.turbogears.org/cogbin.html) |
| 160 | + The CogBin is a list of the most common pluggable applications for |
| 161 | + TurboGears, it enlists ready made pieces you can plug into your |
| 162 | + web application to provide features like Facebook Login, |
| 163 | + Comments, Registration and so on... |
| 164 | + |
| 165 | +* [React in Pure Python](https://medium.com/@__amol__/es2015-and-react-in-pure-python-environment-b326dc15012c) |
| 166 | + An article showcasing how to create web applications relying on React |
| 167 | + without the need to have NodeJS installed at all. The article |
| 168 | + uses TurboGears as the web framework to develop the example application. |
0 commit comments