Mercurial > genshi > genshi-test
annotate 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 |
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 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
8 loader = TemplateLoader( |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
9 os.path.join(os.path.dirname(__file__), '..', 'templates'), |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
10 auto_reload=True |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
11 ) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
12 |
615
0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
611
diff
changeset
|
13 def output(filename, method='html', encoding='utf-8', **options): |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
14 """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
|
15 for rendering, and which serialization method and options should be |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
16 applied. |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
17 """ |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
18 def decorate(func): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
19 def wrapper(*args, **kwargs): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
20 cherrypy.thread_data.template = loader.load(filename) |
615
0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
611
diff
changeset
|
21 if method == 'html': |
0dc152d128f5
GenshiTutorial: make URLs dynamic so that the app could theoretically be mounted on some other SCRIPT_NAME.
cmlenz
parents:
611
diff
changeset
|
22 options.setdefault('doctype', 'html') |
611
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
23 serializer = get_serializer(method, **options) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
24 stream = func(*args, **kwargs) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
25 if not isinstance(stream, Stream): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
26 return stream |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
27 return encode(serializer(stream), method=serializer, |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
28 encoding=encoding) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
29 return wrapper |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
30 return decorate |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
31 |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
32 def render(*args, **kwargs): |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
33 """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
|
34 ``@output`` decorator. |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
35 """ |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
36 if args: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
37 assert len(args) == 1, \ |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
38 'Expected exactly one argument, but got %r' % (args,) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
39 template = loader.load(args[0]) |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
40 else: |
16b1be35c265
Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff
changeset
|
41 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
|
42 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
|
43 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
|
44 return template.generate(ctxt) |