Mercurial > genshi > genshi-test
comparison examples/tutorial/geddit/lib/template.py @ 615:0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
author | cmlenz |
---|---|
date | Wed, 29 Aug 2007 20:12:54 +0000 |
parents | 16b1be35c265 |
children | dba522b4c31d |
comparison
equal
deleted
inserted
replaced
613:d8f961381f75 | 615:0dc152d128f5 |
---|---|
1 import os | 1 import os |
2 | 2 |
3 import cherrypy | 3 import cherrypy |
4 from genshi.core import Stream | 4 from genshi.core import Stream |
5 from genshi.output import encode, get_serializer | 5 from genshi.output import encode, get_serializer |
6 from genshi.template import TemplateLoader | 6 from genshi.template import Context, TemplateLoader |
7 | 7 |
8 loader = TemplateLoader( | 8 loader = TemplateLoader( |
9 os.path.join(os.path.dirname(__file__), '..', 'templates'), | 9 os.path.join(os.path.dirname(__file__), '..', 'templates'), |
10 auto_reload=True | 10 auto_reload=True |
11 ) | 11 ) |
12 | 12 |
13 def output(filename, method=None, encoding='utf-8', **options): | 13 def output(filename, method='html', encoding='utf-8', **options): |
14 """Decorator for exposed methods to specify what template the should use | 14 """Decorator for exposed methods to specify what template the should use |
15 for rendering, and which serialization method and options should be | 15 for rendering, and which serialization method and options should be |
16 applied. | 16 applied. |
17 """ | 17 """ |
18 def decorate(func): | 18 def decorate(func): |
19 def wrapper(*args, **kwargs): | 19 def wrapper(*args, **kwargs): |
20 cherrypy.thread_data.template = loader.load(filename) | 20 cherrypy.thread_data.template = loader.load(filename) |
21 if method == 'html': | |
22 options.setdefault('doctype', 'html') | |
21 serializer = get_serializer(method, **options) | 23 serializer = get_serializer(method, **options) |
22 stream = func(*args, **kwargs) | 24 stream = func(*args, **kwargs) |
23 if not isinstance(stream, Stream): | 25 if not isinstance(stream, Stream): |
24 return stream | 26 return stream |
25 return encode(serializer(stream), method=serializer, | 27 return encode(serializer(stream), method=serializer, |
35 assert len(args) == 1, \ | 37 assert len(args) == 1, \ |
36 'Expected exactly one argument, but got %r' % (args,) | 38 'Expected exactly one argument, but got %r' % (args,) |
37 template = loader.load(args[0]) | 39 template = loader.load(args[0]) |
38 else: | 40 else: |
39 template = cherrypy.thread_data.template | 41 template = cherrypy.thread_data.template |
40 return template.generate(**kwargs) | 42 ctxt = Context(url=cherrypy.url) |
43 ctxt.push(kwargs) | |
44 return template.generate(ctxt) |