Personal tools

Discussion

The tutorial is deliberately short to make it more approachable but there are of course lots of other interesting options to consider.
As an example application we're going to develop a very simple site that uses macros and then later add a layer dedicated for mobile phone browsing without having to put in "if" statements in your templates to accommodate for the differences you'll want to apply.
Page 4 of 4.

A lightweight alternative to WURFL is MUA which stands for "Mobile User Agent" and is a project that's come out of Google.

"Detects whether a given User-Agents HTTP header corresponds to a mobile phone, and attempts to extract the vendor/model from it. In Python."

Here's how you could use it:

from MobileUserAgent import parseUserAgent

@grok.subscribe(Webpage, IBeforeTraverseEvent)
def handle(obj, event):
    if parseUserAgent(event.request.get('HTTP_USER_AGENT')):
        applySkin(event.request, MobileLayer)

The objective of this tutorial is to show how to use layers to your advantage. A mobile layer is just one way of doing it of course. There are plenty other use cases. For example, you want to apply a different layer based on logged in user credentials. Or you have a complex CMS with expensive rendering and you want an alternative view that is much simpler and faster and can sustain being dugg.

Another example is when you want to use the same application an add a REST interface on top of it. A point worth making is that the MobileLayer we define in this example application inherits from IDefaultBrowserLayer. That means that it is able to fall back on all the default that comes out of the box of Grok such as the error page you get when you request a page that does not exist. An alternative to this is to create a layer that inherits from grok.IGrokLayer which means you have nothin to fall back on and this can be useful when, for example, you want to have complete control over the error messages and you don't let people request views that aren't defined in the layer.