Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Rawdog is flexible: using Mako templates

Wh­y? It's ba­si­ca­lly un­do­cu­men­te­d, it looks ugly and it does­n't su­pport tem­pla­te inhe­ri­tan­ce, whi­ch in this ca­se is ve­ry use­fu­l, be­cau­se I am ac­tua­lly doing two ve­ry si­mi­lar pla­ne­ts: 1 2.

So, sin­ce I saw a plu­gin to use Ve­llum tem­pla­tes, I thou­gh­t, why not use my fa­vou­ri­te tem­pla­ting li­bra­ry (Mako) ins­tea­d?

It turns out to be ve­ry ea­sy to do!

Just put this in your plu­gins fol­der and start using Mako tem­pla­tes (ye­s, the pla­ne­t's con­fig and stu­ff is in gi­thub. Why no­t?).

# -*- 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)

Yu­p, 20 li­nes of co­de!


Contents © 2000-2024 Roberto Alsina