wiki:NonPackagedKidTemplates
Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 2 (modified by robinbryce, 13 years ago) (diff)

--

Motivation

You want to deploy a site that uses kid templates in a manner that suites more traditional web deployment models. For example, you are working with web developers who are comfortable with html templating, possibly are familiar with kid, but are not python developers. Additionaly you may have very specific requirements with regard to how your site content is hosted, permissions, existing cacheing systems etc. For these reasons it is more convenient to support a site directory to which kid template files (.kid) can be uploaded to. The standard mechanisms for integrating kid templates with turbogears controlers look like this:

class Root(controllers.RootControllers):
    @expose(template='yourpythonapp.templates.welcome')
    def index(self):
        import time
        return dict(now=time.time())

This uses the TurboKid? plugin to load templates from a python package that you must provide to wrap the templates. This example enables you to rewrite the above as:

class Root(controllers.RootControllers):
    
    # bigsitesupport is the name you gave your turbogears application when you set it up (tg-admin quickstart)

    @expose(template='bigsitesupport/kid/welcome')
    def index(self):
        import time
        return dict(now=time.time())

And have welcome.kid loaded from the file system (possibly via custom caching machinery) without the need to bundle the template in a python package. It does this using a technique that is compatible with TurboKid? and the existing TurboGears template engine machinery.

The python import hook

First create an import hook that is compatible with PEP 302  http://www.python.org/dev/peps/pep-0302/. This enables your customisation to be picked up by TruboKid? with out needing to patch its source. This example is derived from the import machinery that exists in kid.importer  http://www.kid-templating.org/trac/browser/trunk/kid/importer.py.

import os, logging
from sys import path_hooks, path_importer_cache
from kid.importer import import_template

log = logging.getLogger('bigsitesupport.kidimport')

class _Importer(object):
    def __init__(self, config, path=None):
        self.config = config
        # assumes you have used the turbogears [/static] directory to configure the location 
        # of your non packaged kid templates. you could quite reasonably introduce a completely new url
        # mapping for this purpose. using the existing [/static] lets you get things working with less work. 
        self.confstatic=config.get('/static')
        self.staticdir=self.confstatic and self.confstatic.get(
                'static_filter.dir', None)
        self.path = path

    def find_module(self, fullname):
        log.info(
            "CONSIDERING: fullname='%s' initpath='%s' ***" % (
                fullname,self.path))
        if not self.staticdir:
            return
        parts=fullname.split('/', 1)
        if len(parts) != 2:
            log.info("REJECTING: not a uri, [%s]" % fullname)
            return
        filename=os.path.join(self.staticdir, parts[1].replace('/',os.sep))
        filename+='.kid'
        if not os.path.exists(filename):
            log.info("REJECTING: file does not exist [%s]" % filename)
            return
        self._filename = filename
        return self

    def load_module(self, fullname):
        # A production quality implementation would integrate with your custom 
        # caching solution in this method. This expositional implementation will
        # ** force ** recompilation of the  template on every access. Which is fine 
        # for development but not for the real world.

        return import_template(fullname, self._filename, force=1)
        

def Importer_factory(config):
    """Factory method to make turbogears config available to the hook"""
    def factory(*args):
        return _Importer(config,*args)
    return factory

_installed=False
def install_import_hook(factory):
    global _installed
    if not _installed:
        path_hooks.append(factory)
        _installed=True
        path_importer_cache.clear()

def remove_import_hook():
    i = 0
    while i < len(path_hooks):
        if isinstance(path_hooks[i], _Importer):
            del path_hooks[i]
        else:
            i += 1
    path_importer_cache.clear()