# Templating

Pydap uses an experimental backend-agnostic templating API for rendering HTML and XML by responses. Since the API is neutral Pydap can use any templating engine, like Mako or Genshi, and templates can be loaded from disk, memory or a database. The server that comes with Pydap, for example, defines a `renderer` object that loads Genshi templates from a directory `templatedir`:

    from pydap.util.template import FileLoader, GenshiRenderer

    class FileServer(object):
        def __init__(self, templatedir, ...):
            loader = FileLoader(templatedir)
            self.renderer = GenshiRenderer(options={}, loader=loader)
            ...

        def __call__(self, environ, start_response):
            environ.setdefault('pydap.renderer', self.renderer)
            ...

And here is how the HTML response uses the renderer: the response requests a template called `html.html`, that is loaded from the directory by the `FileLoader` object, and renders it by passing the context:

    def serialize(dataset):
        ...
        renderer = environ['pydap.renderer']
        template = renderer.loader('html.html')
        output = renderer.render(template, context, output_format='text/html')
        return [output]

(This is acutally a simplification; if you look at the code you’ll notice that there’s also code to fallback to a default renderer if one is not found in the `environ`.)