[Grok-dev] Re: Problem with applySkin (Grok 0.12)

Philipp von Weitershausen philipp at weitershausen.de
Fri Jul 11 08:39:54 EDT 2008


Sebastian Ware wrote:
> The skins are working fine with "++skin++mobile" but I can't get the 
> applySkin to work (I am using grok 0.12). Basically nothing happens, no 
> error, nothing. It just keeps on using the default skin. And I have 
> checked that the applySkin method is actually called.
> 
>   #############################
>  ###      Mobile skin      ###
> #############################
> 
> from zope.publisher.interfaces.browser import IDefaultBrowserLayer
> class MobileLayer(IDefaultBrowserLayer):
>     pass
> 
> class MobileSkin(grok.Skin):
>     grok.name('mobile')
>     grok.layer(MobileLayer)
> 
> class MobileTemplate(grok.View):
>     grok.layer(MobileLayer)
>     grok.context(Interface)
> 
> from zope.app.publication.interfaces import IBeforeTraverseEvent
> from zope.publisher.browser import applySkin
> @grok.subscribe(grok.View, IBeforeTraverseEvent)
> def handle(obj, event):
>     if event.request.get('HTTP_USER_AGENT').find('Safari') > -1:
>         applySkin(event.request, MobileLayer)

You're subscribing to the wrong object. Views are rarely traversed over 
(e.g. someobj/@@someview/anothersegment). They are only traversed *to* 
(e.g. someobj/@@someview).

You want to apply the skin to all views, I simply suggest doing so when 
traversing over the object that will always be traversed over: the 
application object:


   class MyApp(grok.Application, ...):
       ...

   ...

   @grok.subscribe(MyApp, IBeforeTraverseEvent)
   def handle(obj, event):
       ...


More information about the Grok-dev mailing list