Implemented and documented "joiner"
[jinja2.git] / examples / profile.py
1 try:
2     from cProfile import Profile
3 except ImportError:
4     from profile import Profile
5 from pstats import Stats
6 from jinja2 import Environment as JinjaEnvironment
7
8 context = {
9     'page_title': 'mitsuhiko\'s benchmark',
10     'table': [dict(a=1,b=2,c=3,d=4,e=5,f=6,g=7,h=8,i=9,j=10) for x in range(1000)]
11 }
12
13 source = """\
14 <!doctype html>
15 <html>
16   <head>
17     <title>${page_title|e}</title>
18   </head>
19   <body>
20     <div class="header">
21       <h1>${page_title|e}</h1>
22     </div>
23     <div class="table">
24       <table>
25       % for row in table
26         <tr>
27         % for cell in row
28           <td>${cell}</td>
29         % endfor
30         </tr>
31       % endfor
32       </table>
33     </div>
34   </body>
35 </html>\
36 """
37 jinja_template = JinjaEnvironment(
38     line_statement_prefix='%',
39     variable_start_string="${",
40     variable_end_string="}"
41 ).from_string(source)
42 print jinja_template.environment.compile(source, raw=True)
43
44
45 p = Profile()
46 p.runcall(lambda: jinja_template.render(context))
47 stats = Stats(p)
48 stats.sort_stats('time', 'calls')
49 stats.print_stats()