tgext.mobilemiddleware / README.rst

About Mobile Middleware

Mobile Middleware is a middleware for WSGI applications.

Thought to be used on Turbogears 2 applications it detects mobile browser and provides a way to detect and react to them. Detection expression can be customized and action can change from plain detection to exposing a specific custom template for mobile requests

You will be able to see if your request is coming from a mobile browser with:

from tg import request
request.is_mobile

Enabling Mobile Agents Detection

In your application config/middleware.py import MobileMiddleware:

from tgext.mobilemiddleware import MobileMiddleware

Change your make_app method:

app = make_base_app(global_conf, full_stack=True, **app_conf)
return MobileMiddleware(app, app_conf)

Exposing Mobile Templates

tgext.mobilemiddleware implements a @expose_mobile decorator that works like @expose TurboGears2 decorator which can be used to specify which template to expose for mobie requests.

This will work by registering a custom template renderer with 'mobile' custom format. @expose_expose supports the same template naming convention that @expose uses and can accept any rendering engine that has been registered in turbogears by specifing it as engine:module.template_name

Examples:

@expose('app.templates.index')
@expose_mobile('app.templates.mobile.index')
def index(self, *args, **kw):
    return dict()

Customizing User Agents Detection

MobileMiddleware by default checks user agent with a regular expression that can be customized by changing [app:main] section of your configuration file by adding something like:

mobile.agents = android|fennec|iemobile|iphone|ipod|ipad
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.