view examples/tutorial/geddit/lib/template.py @ 611:16b1be35c265

Add current code for GenshiTutorial to the `examples` directory.
author cmlenz
date Wed, 29 Aug 2007 17:51:45 +0000
parents
children 0dc152d128f5
line wrap: on
line source
import os

import cherrypy
from genshi.core import Stream
from genshi.output import encode, get_serializer
from genshi.template import TemplateLoader

loader = TemplateLoader(
    os.path.join(os.path.dirname(__file__), '..', 'templates'),
    auto_reload=True
)

def output(filename, method=None, encoding='utf-8', **options):
    """Decorator for exposed methods to specify what template the should use
    for rendering, and which serialization method and options should be
    applied.
    """
    def decorate(func):
        def wrapper(*args, **kwargs):
            cherrypy.thread_data.template = loader.load(filename)
            serializer = get_serializer(method, **options)
            stream = func(*args, **kwargs)
            if not isinstance(stream, Stream):
                return stream
            return encode(serializer(stream), method=serializer,
                          encoding=encoding)
        return wrapper
    return decorate

def render(*args, **kwargs):
    """Function to render the given data to the template specified via the
    ``@output`` decorator.
    """
    if args:
        assert len(args) == 1, \
            'Expected exactly one argument, but got %r' % (args,)
        template = loader.load(args[0])
    else:
        template = cherrypy.thread_data.template
    return template.generate(**kwargs)
Copyright (C) 2012-2017 Edgewall Software