def test_complex(self): tpl = """Hello World {% for i in abc%} x {{i}} {% endfor %} That's all!""" t = Template(tpl) t.render(abc="123")
def test_context_inheritance(self): base = Template("HEAD {% for i in '123' %}" "{%slot%}xxx{%endslot%}" "{%endfor%} FOOTER") final = Template("body {{i}}", parent=base) res = final.render() assert res == "HEAD body 1body 2body 3 FOOTER"
def test_2levels(self): base = Template("HEAD {%slot%}xxx{%endslot%} FOOTER") first = Template("This is the body {% slot %} yyy {% endslot %}", parent=base) second = Template("The end...", parent=first) res = second.render() assert res == "HEAD This is the body The end... FOOTER"
def test_loop(self): tpl = Template("{% for i in seq%}" "{%if loop.first%}{{i}} is first{%endif%}" "{%if loop.last%}{{i}} is last{%endif%}" "{{i}}{{loop.index}}-{{loop.index0}}" "{%endfor%}") res = tpl.render(seq="abcde") assert res == "a is firsta0-1b1-2c2-3d3-4e is laste4-5"
def test_explicit3(self): base = Template("HEAD {%slot main%}xxx{%endslot%} FOOTER") final = Template( "{%fill main%}This is the body{%endfill%}" "{%fill other%}Other fill{%endfill%}", parent=base) res = final.render() assert res == "HEAD This is the body FOOTER"
def test_multi_fill_sideeffect(self): """ if the child does not explicitly define a fill, it will be used for any / all slots """ base = Template("HEAD {%slot main%}xxx{%endslot%} - " "{% slot bar %}yyy{%endslot%} FOOTER") final = Template("Hello", parent=base) res = final.render() assert res == "HEAD Hello - Hello FOOTER"
def test_multi_fill(self): base = Template("HEAD {%slot main%}xxx{%endslot%} - " "{% slot bar %}yyy{%endslot%} FOOTER") final = Template( "Hello " "{%fill main %}This is the main block" "{% endfill %} and " "{%fill bar %}this is the bar block" "{%endfill %}", parent=base) res = final.render() assert res == "HEAD This is the main block - this is the bar block FOOTER"
def test_total_madness1(self): base = Template("HEAD {%slot a%} aa {% endslot %}" "{%for i in '123'%}" "{%slot b %}{%endslot%}" "{%endfor%}" "FOOTER") c1 = Template( "{%fill a%}A{%endfill%}" "noise" "{%fill b%}" "{{i}}{%slot c%}..{%endslot%}" "{%endfill%}", parent=base) c2 = Template("Default", parent=c1) res = c2.render() assert res == "HEAD A1Default2Default3DefaultFOOTER"
def test_attraction_marker2(self): base = Template(""" <html> <style> p, th, td { font-family: 'Open Sans', sans-serif; } </style> <body> {% slot content %}{% endslot %} </body> </html> """) final = Template("<<MARKER>>", parent=base) res = final.render() assert '<<MARKER>>' in res
def test_string_expression_special2(self): tpl = Template("{{'{%hello%}'}}") assert tpl.render() == '{%hello%}'
def test_string_expression(self): tpl = Template("{{'hello'}}") assert tpl.render() == 'hello'
def test_expression(self): tpl = Template("{{i + j}}") assert tpl.render(i=10, j=5) == "15" assert tpl.render(i="Hello", j="World") == "HelloWorld"
def test_interpolation(self): tpl = Template("{{i}}") assert tpl.render(i=10) == "10" assert tpl.render(i=0) == "0" assert tpl.render(i=-10) == "-10" assert tpl.render(i="hello") == "hello"
def test_nondefault(self): base = Template("HEAD {%slot foo%}xxx{%endslot%} FOOTER") final = Template("{%fill foo%}This is the body{%endfill%}", parent=base) res = final.render() assert res == "HEAD This is the body FOOTER"
def test_context_stacking(self): """ this is more of a context feature """ tpl = Template("{%for i in j%}{{i}}{%endfor%}{{i}}") assert tpl.render(i="z", j=["a"]) == "az"
def test_simple(self): tpl = Template("{%for i in j%}{{i}}{% endfor %}") assert tpl.render(j="a") == "a" assert tpl.render(j=[1, 2, 3]) == "123"
def test_missing(self): base = Template("HEAD {%slot foo%}xxx{%endslot%} FOOTER") final = Template("{%fill main%}This is the body{%endfill%}", parent=base) res = final.render() assert res == "HEAD xxx FOOTER"
def test_else(self): tpl = Template("{%if bool%}TRUE{%else%}FALSE{%endif%}") assert tpl.render(bool=True) == "TRUE" assert tpl.render(bool=False) == "FALSE"
def test_simple(self): base = Template("HEAD {%slot%}xxx{%endslot%} FOOTER") final = Template("This is the body", parent=base) res = final.render() assert res == "HEAD This is the body FOOTER"
def test_simple(self): tpl = Template("{%if bool%}TRUE{%endif%}") assert tpl.render(bool=True) == "TRUE" assert tpl.render(bool=False) == ""
def test_attraction_marker(self): base = Template("HEAD {% slot content %}xxx{% endslot %} FOOTER") final = Template("<<MARKER>>", parent=base) res = final.render() assert res == "HEAD <<MARKER>> FOOTER"