Home Company Services Portfolio Contact us nav spacer

Plone skin switching by inspecting the URL

by hedley posted on Oct 26, 2007 10:59 AM last modified Oct 26, 2007 10:59 AM

Does your site have one view for the public interface and another for the admin interface? Then you should serve different skins depending on the URL.

Plone's CSS and main_template and friends are not easily adapted to very specific public interfaces. In such a case it is better to serve a skin called say Public to normal site visitors and Plone Default to administrators. I assume you know how to create these skins.

Now you need to know which skin to serve. Let http://www.foo.com be the URL for use by normal site visitors, and http://admin.foo.com be the URL for use by site administrators.

We are going to use an External Method and a Set Access Rule to accomplish this.

In your product's Extension directory create a file called selectSkin.py

# Contents of selectSkin.py
def run(self):
request = self.REQUEST
if request.get('URL').startswith('http://admin'):
self.portal_url.getPortalObject().changeSkin('Plone Default')
else:
self.portal_url.getPortalObject().changeSkin('Public')

Add this snippet to Install.py or AppInstall.py. Replace MyProduct with the name of your product.
from Products.SiteAccess.AccessRule import manage_addAccessRule
if not hasattr(self, 'selectSkin'):
em = self.manage_addProduct['ExternalMethod']
em.manage_addExternalMethod('selectSkin', 'selectSkin', 'MyProduct.selectSkin', 'run')
manage_addAccessRule(self, 'selectSkin')
Browse to your site via different URLs and you'll see it in action.