示例#1
0
from __future__ import print_function

from jinja import Environment

tmpl = Environment().from_string("""\
<ul>
{%- for item in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] if item % 2 == 0 %}
    <li>{{ loop.index }} / {{ loop.length }}: {{ item }}</li>
{%- endfor %}
</ul>
if condition: {{ 1 if foo else 0 }}
""")
print(tmpl.render(foo=True))
示例#2
0
    {%- endfor %}
    {%- if row|length < 3 %}
    <td colspan="{{ 3 - (row|length) }}">&nbsp;</td>
    {%- endif %}
  </tr>
  {%- endfor %}
</table>

<h1>Macros</h1>
{% macro foo seq %}
  <ul>
  {%- for item in seq %}
    <li>{{ caller(item=item) }}</li>
  {%- endfor %}
  </ul>
{% endmacro %}

{% call foo(items) -%}
  [{{ item }}]
{%- endcall %}
''')

start = time()
for _ in xrange(50):
    tmpl.render(
        items=range(200),
        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)]
    )
print time() - start