webwareskins

Problem

Have a website with different skins (depending on domain, user, etc.)

Solution

This is a variant of the containment approach. You have to create the following directory structure (and place it somewhere in the Python Path):

ServerTemplates_
              |
              |-- __init__.py
              |-- TemplateFactory_.py
              |-- Generic
              |--Domain1
              |--Domain2
              |--DomainN

TemplateFactory looks like this:

import os,sys

class TemplateStore_:
     def __init__(self,site = 'Generic'):
              self.template = {}
              self._site = site
              if not os.path.isdir(os.path.join('/usr/local/Intranet/ServerTemplate_',site)):
                            raise "Template directory %s is not available" % site

     def site(self):
              return self._site

     def getTemplate(self,tempName,searchList=[]):
              if self.template.has_key(tempName):
                            template = self.template[tempName]
              else:
                            modname = 'ServerTemplate_.'+self.site()+'.'+tempName
                            try:
                                     __import__(modname)
                            except ImportError_:
                                     raise ImportError_('no module named %s' % modname)
                            template = getattr(sys.modules[modname],tempName)()
                            self.template[tempName] = template

              tsearch = template.searchList()
              for item in searchList:
                            if item not in tsearch:
                                     #template.addToSearchList_(item) #changed 26/11/2002
                                     tsearch.insert(0,item)

              return template

In your Site servlet (maybe SidebarPage or something similar) you have the following additional methods:

def initTmplStore(self):
              tmplStore = {}
              for server in siteList: #siteList contains a list of your sites
                            if not tmplStore.has_key(server):
                                     tmplStore[server] = TemplateStore(server)

              return tmplStore

def getTemplate(self,name,searchList=[]):
     try:
              tmpl = self._templates[self.getSite()].getTemplate(name,searchList)
     except:
              tmpl = self._templates['Generic'].getTemplate(name,searchList)

def getSite(self):
     return self.request().environ()['SERVER_ADDR']

Now, any servlet that inherits from your base site, just calls getTemplate("myTemplateName") and automatically gets the right template

This can only work of course, if all "DomainN" directories contain the same named (Cheetah) Templates.

Every site can have of course its own CSS style sheet.

This approach could be used as well for other kinds of situations where you need to decide on runtime which template to use. User preferences come to mind.

-- StephanDiehl - 02 Nov 2002

---

Changed the code a little bit since "addToSearchList" is not longer present

-- StephanDiehl - 26 Nov 2002