annotate examples/tutorial/geddit/lib/template.py @ 625:dba522b4c31d

GenshiTutorial: implemented AJAX commenting.
author cmlenz
date Fri, 31 Aug 2007 17:01:00 +0000
parents 0dc152d128f5
children
rev   line source
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
1 import os
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
2
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
3 import cherrypy
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
4 from genshi.core import Stream
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
5 from genshi.output import encode, get_serializer
615
0dc152d128f5 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
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
7
625
dba522b4c31d GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
8 from geddit.lib import ajax
dba522b4c31d GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
9
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
10 loader = TemplateLoader(
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
11 os.path.join(os.path.dirname(__file__), '..', 'templates'),
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
12 auto_reload=True
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
13 )
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
14
615
0dc152d128f5 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
16b1be35c265 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
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
17 for rendering, and which serialization method and options should be
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
18 applied.
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
19 """
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
20 def decorate(func):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
21 def wrapper(*args, **kwargs):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
22 cherrypy.thread_data.template = loader.load(filename)
625
dba522b4c31d GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
23 opt = options.copy()
dba522b4c31d GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
24 if not ajax.is_xhr() and method == 'html':
dba522b4c31d GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
25 opt.setdefault('doctype', 'html')
dba522b4c31d GenshiTutorial: implemented AJAX commenting.
cmlenz
parents: 615
diff changeset
26 serializer = get_serializer(method, **opt)
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
27 stream = func(*args, **kwargs)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
28 if not isinstance(stream, Stream):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
29 return stream
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
30 return encode(serializer(stream), method=serializer,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
31 encoding=encoding)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
32 return wrapper
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
33 return decorate
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
34
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
35 def render(*args, **kwargs):
16b1be35c265 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
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
37 ``@output`` decorator.
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
38 """
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
39 if args:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
40 assert len(args) == 1, \
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
41 'Expected exactly one argument, but got %r' % (args,)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
42 template = loader.load(args[0])
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
43 else:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
44 template = cherrypy.thread_data.template
615
0dc152d128f5 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)
0dc152d128f5 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)
0dc152d128f5 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