Personal tools

Publishing the layer

Views are registered with layers and layers are published by skins.
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 3 of 4.

The most obvious way to differentiate which skin should be applied is to include /++skin++mobile/ in the URL or not. This works well and keeps things clear and obvious. When you deploy your web application you will probably put some sort of server in front of Zope like Apache, Nginx or Varnish. All of these support rewriting or proxying so for example, for you redirect it like this:

RewriteEngine On
RewriteRule ^(.*) http://localhost:8080/app/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]

And following, for the other sub-domain you redirect it like this:

RewriteEngine On
RewriteRule ^(.*) http://localhost:8080/++skin++mobile/app/++vh++http:%{SERVER_NAME}:80/++/$1 [P,L]

But suppose you don't want to use different URLs but instead sniff the HTTP user agent and depending on what you find you apply a different skin on the fly. The best way to do this properly is to use WURFL but for the sake of simplicity we'll do a very basic script.

The key to switching skin depending on certain parameters is to subscribe to the IBeforeTraverseEvent and write a clever function that does the actual business logic. Add the following to your

from import IBeforeTraverseEvent
from zope.publisher.browser import applySkin

@grok.subscribe(Webpage, IBeforeTraverseEvent)
def handle(obj, event):
    if event.request.get('HTTP_USER_AGENT').find('Nokia') > -1:
        applySkin(event.request, MobileLayer)

Don't let the stupidity of the if statement and the strangeness of the zope imports frighten you. At least there's not a single underscore in this code. Apart from extending this with some smart WURFL code you can of course bake in some sort of checks on cookies. Another thing you might want to consider is to do a redirect. Plus WURFL can give you other pieces of information based on the HTTP user agent that you can use to your advantage. For example you can modify the request with information about the screen size when applying the layer and you would then use this information to show different sized thumbnails if your application has that.