comparison 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
comparison
equal deleted inserted replaced
610:6a37018199fd 611:16b1be35c265
1 import os
2
3 import cherrypy
4 from genshi.core import Stream
5 from genshi.output import encode, get_serializer
6 from genshi.template import TemplateLoader
7
8 loader = TemplateLoader(
9 os.path.join(os.path.dirname(__file__), '..', 'templates'),
10 auto_reload=True
11 )
12
13 def output(filename, method=None, encoding='utf-8', **options):
14 """Decorator for exposed methods to specify what template the should use
15 for rendering, and which serialization method and options should be
16 applied.
17 """
18 def decorate(func):
19 def wrapper(*args, **kwargs):
20 cherrypy.thread_data.template = loader.load(filename)
21 serializer = get_serializer(method, **options)
22 stream = func(*args, **kwargs)
23 if not isinstance(stream, Stream):
24 return stream
25 return encode(serializer(stream), method=serializer,
26 encoding=encoding)
27 return wrapper
28 return decorate
29
30 def render(*args, **kwargs):
31 """Function to render the given data to the template specified via the
32 ``@output`` decorator.
33 """
34 if args:
35 assert len(args) == 1, \
36 'Expected exactly one argument, but got %r' % (args,)
37 template = loader.load(args[0])
38 else:
39 template = cherrypy.thread_data.template
40 return template.generate(**kwargs)
Copyright (C) 2012-2017 Edgewall Software