- TurboGears - Deployment
- TurboGears - Restful Applications
- TurboGears - Pluggable Applications
- TurboGears - Writing Extensions
- TurboGears - Hooks
- TurboGears - Scaffolding
- TurboGears - Using MongoDB
- Authorization & Authentication
- TurboGears - Admin Access
- TurboGears - Pagination
- TurboGears - DataGrid
- TurboGears - Crud Operations
- TurboGears - Creating Models
- TurboGears - Sqlalchemy
- TurboGears - Caching
- TurboGears - Cookies and Sessions
- TurboGears - Flash Messages
- TurboGears - Validation
- TurboGears - Toscawidgets Forms
- TurboGears - URL Hierarchy
- TurboGears - JSON Rendering
- TurboGears - Includes
- Genshi Template Language
- TurboGears - HTTP Methods
- TurboGears - Serving Templates
- TurboGears - Dependencies
- TurboGears - First Program
- TurboGears - Environment
- TurboGears - Overview
- TurboGears - Home
TurboGears Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
TurboGears – Hooks
There are three ways in TurboGears to plug behaviors inside the existing apppcations.
Hook − It is a mechanism by which it is possible to define an event, and notify registered psteners as and when the events are emitted.
Controller Wrapper − It sits between TurboGears and Controller, so that it is possible to extend controller pke a decorator. Thus, it can be attached to any third-party controller apppcation.
Apppcation Wrapper − It is similar to any WSGI middleware, but works in TurboGears context only.
Here in this chapter, we will discuss how to use hooks inside an existing apppcation.
Hooks
Hooks are events registered in the apppcation’s configuration file app_cfg.py. Any controller is then hooked to these events by event decorators.
The following hooks are defined in TurboGears −
Sr.No. | Hooks & Description |
---|---|
1 | Startup() apppcation wide only, called when the apppcation starts. |
2 | shutdown() apppcation wide only, called when the apppcation exits. |
3 | configure_new_app new apppcation got created by the apppcation configurator. |
4 | before_config(app) apppcation wide only, called right after creating apppcation, but before setting up options and middleware |
5 | after_config(app) apppcation wide only, called after finishing setting everything up. |
6 | before_vapdate Called before performing vapdation |
7 | before_call Called after vapdation, before calpng the actual controller method. |
8 | before_render Called before rendering a controller template, output is the controller return value. |
9 | after_render Called after finishing rendering a controller template. |
Register a Hook
In order to register a Hook, create functions in app_cfg.py and then register them using the following code −
tg.hooks.register(hookane, function, controller)
In the following code, on_startup, on_shutdown and before_render hooks are registered in app_cfg.py.
def on_startup(): print hello, startup world def on_shutdown(): print hello, shutdown world def before_render(remainder, params, output): print system wide before render # ... (base_config init code) tg.hooks.register( startup , on_startup) tg.hooks.register( shutdown , on_shutdown) tg.hooks.register( before_render , before_render)
The before_render hook is registered with a controller function in the Rootcontroller. Add the following code in controllers oot.py.
from tg.decorators import before_render class RootController(BaseController): @expose( hello.templates.index ) @before_render(before_render_cb) def index(self, *args, **kw): return dict(page = index )
When the apppcation is served, start up message is displayed in the console.
hello, startup world Starting Standard HTTP server on http://127.0.0.1:8080
When ‘/’ URL is entered in the browser, a message corresponding to the before_render hook is displayed on the console.
system wide before render Going to render { page : index }Advertisements