Rawdog is flexible: using Mako templates

I am using rawdog for Planeta PyAr and I am very happy with it. One thing I really didn't like was the templating.

Why? It's basically undocumented, it looks ugly and it doesn't support template inheritance, which in this case is very useful, because I am actually doing two very similar planets: 1 2.

So, since I saw a plugin to use Vellum templates, which means the templating is pluggable, why not use my favourite templating library (Mako) instead?

It turns out to be very easy to do!

Just put this in your plugins folder and start using Mako templates (yes, the planet's config and stuff is in github. Why not?).

# -*- coding: utf-8 -*-
import rawdoglib.plugins

from mako.template import Template
from mako.lookup import TemplateLookup
from mako import exceptions

class MakoPlugin:
    def __init__(self):
        self.lookup = TemplateLookup(directories=['.'],\
                              output_encoding='utf-8',\
                              encoding_errors='replace')

    def fill_template(self,template, bits, result):
        self.lookup.put_string('actual',template)
        t=self.lookup.get_template('actual')
        try:
            r=t.render_unicode(**bits)
        except:
            r=exceptions.html_error_template().render()
        result.value=r
        return False

p = MakoPlugin()
rawdoglib.plugins.attach_hook("fill_template",\
                              p.fill_template)

Yup, 20 lines of code!

Comments

Comments powered by Disqus