Mercurial > genshi > mirror
annotate examples/tutorial/geddit/lib/template.py @ 712:b33c14a1f579 experimental-match-fastpaths
minor performance updates for non-match cases like bigtable, and for any case where before_template/after_template would create an empty MatchSet
author | aflett |
---|---|
date | Mon, 07 Apr 2008 18:27:55 +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 | 8 from geddit.lib import ajax |
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 | 23 opt = options.copy() |
24 if not ajax.is_xhr() and method == 'html': | |
25 opt.setdefault('doctype', 'html') | |
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) |