annotate examples/includes/run.py @ 439:9f11c745fac9 trunk

Add support for adding custom template filters by passing a custom callback function to the `TemplateLoader`. Closes #89 (see added unit test).
author cmlenz
date Mon, 02 Apr 2007 19:43:31 +0000
parents 84168828b074
children
rev   line source
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
1 #!/usr/bin/python
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
3
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
4 import os
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
5 import sys
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
6 import timing
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
7
230
84168828b074 Renamed Markup to Genshi in repository.
cmlenz
parents: 23
diff changeset
8 from genshi.template import Context, TemplateLoader
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
9
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
10 def test():
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
11 base_path = os.path.dirname(os.path.abspath(__file__))
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
12 loader = TemplateLoader([os.path.join(base_path, 'skins'),
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
13 os.path.join(base_path, 'module'),
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
14 os.path.join(base_path, 'common')])
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
15
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
16 timing.start()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
17 tmpl = loader.load('test.html')
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
18 timing.finish()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
19 print ' --> parse stage: %dms' % timing.milli()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
20
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
21 data = dict(hello='<world>', skin='default', hey='ZYX', bozz=None,
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
22 items=['Number %d' % num for num in range(1, 15)])
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
23
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
24 print tmpl.generate(Context(**data)).render(method='html')
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
25
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
26 times = []
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
27 for i in range(100):
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
28 timing.start()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
29 list(tmpl.generate(Context(**data)))
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
30 timing.finish()
22
2483fe549959 Fix for the template engine plugin: the search path is now ignored if the requested template path is absolute.
cmlenz
parents: 21
diff changeset
31 times.append(timing.milli())
21
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
32 sys.stdout.write('.')
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
33 sys.stdout.flush()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
34 print
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
35
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
36 print ' --> render stage: %dms (avg), %dms (min), %dms (max)' % (
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
37 sum(times) / len(times), min(times), max(times))
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
38
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
39 if __name__ == '__main__':
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
40 if '-p' in sys.argv:
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
41 import hotshot, hotshot.stats
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
42 prof = hotshot.Profile("template.prof")
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
43 benchtime = prof.runcall(test)
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
44 stats = hotshot.stats.load("template.prof")
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
45 stats.strip_dirs()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
46 stats.sort_stats('time', 'calls')
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
47 stats.print_stats()
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
48 else:
b4d17897d053 * Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
diff changeset
49 test()
Copyright (C) 2012-2017 Edgewall Software