annotate examples/tutorial/geddit/lib/template.py @ 1045:d7ede0cf4735 stable-0.6.x tip

Merge r1269 from trunk (fix for selecting namespaced attributes).
author hodgestar
date Thu, 20 Mar 2014 13:01:56 +0000
parents abad7c2ebe15
children
rev   line source
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
1 import os
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
2
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
3 import cherrypy
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
4 from genshi.core import Stream
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
5 from genshi.output import encode, get_serializer
615
06165fee45ab GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents: 611
diff changeset
6 from genshi.template import Context, TemplateLoader
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
7
625
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
8 from geddit.lib import ajax
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
9
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
10 loader = TemplateLoader(
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
11 os.path.join(os.path.dirname(__file__), '..', 'templates'),
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
12 auto_reload=True
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
13 )
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
14
615
06165fee45ab GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents: 611
diff changeset
15 def output(filename, method='html', encoding='utf-8', **options):
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
16 """Decorator for exposed methods to specify what template the should use
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
17 for rendering, and which serialization method and options should be
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
18 applied.
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
19 """
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
20 def decorate(func):
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
21 def wrapper(*args, **kwargs):
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
22 cherrypy.thread_data.template = loader.load(filename)
625
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
23 opt = options.copy()
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
24 if not ajax.is_xhr() and method == 'html':
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
25 opt.setdefault('doctype', 'html')
abad7c2ebe15 GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
26 serializer = get_serializer(method, **opt)
611
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
27 stream = func(*args, **kwargs)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
28 if not isinstance(stream, Stream):
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
29 return stream
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
30 return encode(serializer(stream), method=serializer,
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
31 encoding=encoding)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
32 return wrapper
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
33 return decorate
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
34
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
35 def render(*args, **kwargs):
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
36 """Function to render the given data to the template specified via the
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
37 ``@output`` decorator.
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
38 """
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
39 if args:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
40 assert len(args) == 1, \
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
41 'Expected exactly one argument, but got %r' % (args,)
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
42 template = loader.load(args[0])
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
43 else:
236c351928a2 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
44 template = cherrypy.thread_data.template
615
06165fee45ab GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents: 611
diff changeset
45 ctxt = Context(url=cherrypy.url)
06165fee45ab GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents: 611
diff changeset
46 ctxt.push(kwargs)
06165fee45ab GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents: 611
diff changeset
47 return template.generate(ctxt)
Copyright (C) 2012-2017 Edgewall Software