Using jade with klein
Klein is another python micro web framework that uses twisted's web server (and therefore twisted's event loop system). It looks like this:
from klein import run, route @route('/') def home(request): return 'Hello, world!' run("localhost", 8080)
Jade is an alternate syntax for HTML that's very terse and great for typing and reading. It looks like this:
doctype html
html
head
title
Hello world
body
h1 Hello
ul
each name in friends
li
| Hi #{name}
PyJade is a python implementation of jade. Jade has some template directives in it, which pyjade can render out to other template languages. This means pyjade does its work only once when the template is loaded, and then another template library does the per-page rendering of the template.
I tried jinja2 but the integration didn't work on the first try. With mako templating library it did, though, so here's the result:
import pyjade
from pyjade.ext.mako import preprocessor as mako_preprocessor
from mako.template import Template
from mako.lookup import TemplateLookupfrom klein import Klein
templates = TemplateLookup(directories=['.'],preprocessor=mako_preprocessor)...@app.route('/', methods=['GET'])def index(self, request):t = templates.get_template("index.jade")return t.render(friends=self.friends)
This isn't Klein-specific, of course. It would work with any other web framework that doesn't have a template rendering system that you want to use.