Example – Using Twisted.Web Templates

You can also make easy use of Twisted’s templating system by returning anything that implements IRenderable. For example, returning a t.w.template.Element will make it be rendered, with the result sent as the response body:

from twisted.web.template import Element, XMLString, renderer
from klein import run, route

class HelloElement(Element):
    loader = XMLString((
        '<h1 '
        '>Hello, <span t:render="name"></span>!</h1>'))

    def __init__(self, name):
        self._name = name

    def name(self, request, tag):
        return self._name

def home(request, name='world'):
    return HelloElement(name)

run("localhost", 8080)