--- author: '' category: '' date: 2009/02/24 21:42 description: '' link: '' priority: '' slug: BB779 tags: programming, python title: 'Rawdog is flexible: using Mako templates' type: text updated: 2009/02/24 21:42 url_type: '' --- I am using rawdog_ for `Planeta PyAr`_ and I am very happy with it. One thing I really didn't like was the templating. .. _rawdog: http://offog.org/code/rawdog.html .. _planeta pyar: http://planeta.python.org.ar 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_. .. _1: http://planeta.python.org.ar .. _2: http://planeta.python.org.ar/python.html 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? .. _mako: http://makotemplates.org 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?). .. _github: http://github.com .. _this: http://github.com/ralsina/planeta-pyar/blob/59c6fffe2c52206eea99df6fd3acbff1dd75df71/plugins/90-mako.py .. code-block:: python # -*- 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!