Using Kid together with Colubrid

There are two decorators for using kid from within Colubrid: a cached and an uncached version.

Uncached

import os
from colubrid import HttpResponse
from kid import load_template

searchpath = 'templates'

def render(name):
    path = os.path.join(searchpath, '%s.kid' % name)
    def proxy(f):
        def on_call(*args, **kwargs):
            c = f(*args, **kwargs) or {}
            t = Template(path, '', False)
            for key, value in c.iteritems():
                setattr(t, key, value)
            return HttpResponse(t.serialize())
        return on_call
    return proxy

Cached

import os
from colubrid import HttpResponse
from kid import Template

searchpath = 'templates'

def render(name):
    path = os.path.join(searchpath, '%s.kid' % name)
    def proxy(f):
        def on_call(*args, **kwargs):
            c = f(*args, **kwargs) or {}
            t = Template(file=path, **c)
            return HttpResponse(t.serialize())
        return on_call
    return proxy

Usage

You can use it from any Colubrid application type:

from colubrid import RegexApplication, execute
import time

class MyApplication(RegexApplication):
    urls = [
        (r'^index$', 'index')
    ]

    @render('index')
    def index(self):
        return {
            'now': time.time()
        }

app = MyApplication

if __name__ == '__main__':
    execute()

Example Template

Here the index.kid template from the example above:

<?xml version="1.0" encoding="utf-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://purl.org/kid/ns#">
<body>

<h1>Welcome from the Index</h1>

The current Unix Timestamp is <span py:replace="now">timestamp here</span>

</body>
</html>
Last Change: 10 Sep 2006 17:12:26 | show source