def test_pageargs(self): collection = lookup.TemplateLookup() collection.put_string("base", """ this is the base. <% sorted_ = pageargs.items() sorted_ = sorted(sorted_) %> pageargs: (type: ${type(pageargs)}) ${sorted_} <%def name="foo()"> ${next.body(**context.kwargs)} </%def> ${foo()} """) collection.put_string("index", """ <%inherit file="base"/> <%page args="x, y, z=7"/> print ${x}, ${y}, ${z} """) if compat.py3k: assert result_lines(collection.get_template('index').render_unicode(x=5,y=10)) == [ "this is the base.", "pageargs: (type: <class 'dict'>) [('x', 5), ('y', 10)]", "print 5, 10, 7" ] else: assert result_lines(collection.get_template('index').render_unicode(x=5,y=10)) == [ "this is the base.", "pageargs: (type: <type 'dict'>) [('x', 5), ('y', 10)]", "print 5, 10, 7" ]
def test_strict(self): t = Template(""" % if x is UNDEFINED: undefined % else: x: ${x} % endif """, strict_undefined=True) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 ) l = TemplateLookup(strict_undefined=True) l.put_string("a", "some template") l.put_string("b", """ <%namespace name='a' file='a' import='*'/> % if x is UNDEFINED: undefined % else: x: ${x} % endif """) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 )
def test_dynamic_key_with_funcargs(self): t = Template(""" <%def name="foo(num=5)" cached="True" cache_key="foo_${str(num)}"> hi </%def> ${foo()} """) m = self._install_mock_cache(t) t.render() t.render() assert result_lines(t.render()) == ['hi'] assert m.key == "foo_5" t = Template(""" <%def name="foo(*args, **kwargs)" cached="True" cache_key="foo_${kwargs['bar']}"> hi </%def> ${foo(1, 2, bar='lala')} """) m = self._install_mock_cache(t) t.render() assert result_lines(t.render()) == ['hi'] assert m.key == "foo_lala" t = Template(''' <%page args="bar='hi'" cache_key="foo_${bar}" cached="True"/> hi ''') m = self._install_mock_cache(t) t.render() assert result_lines(t.render()) == ['hi'] assert m.key == "foo_hi"
def test_utf8_format_exceptions_pygments(self): """test that htmlentityreplace formatting is applied to exceptions reported with format_exceptions=True""" l = TemplateLookup(format_exceptions=True) if compat.py3k: l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""") else: l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""") if compat.py3k: assert '<table class="error syntax-highlightedtable"><tr><td '\ 'class="linenos"><div class="linenodiv"><pre>2</pre>'\ '</div></td><td class="code"><div class="error '\ 'syntax-highlighted"><pre><span class="cp">${</span>'\ '<span class="s">'привет'</span> <span class="o">+</span> '\ '<span class="n">foobar</span><span class="cp">}</span>'\ '<span class="x"></span>' in \ result_lines(l.get_template("foo.html").render().decode('utf-8')) else: assert '<table class="error syntax-highlightedtable"><tr><td '\ 'class="linenos"><div class="linenodiv"><pre>2</pre>'\ '</div></td><td class="code"><div class="error '\ 'syntax-highlighted"><pre><span class="cp">${</span>'\ '<span class="s">u'прив'\ 'ет'</span> <span class="o">+</span> '\ '<span class="n">foobar</span><span class="cp">}</span>'\ '<span class="x"></span>' in \ result_lines(l.get_template("foo.html").render().decode('utf-8'))
def test_dynamic_key_with_context(self): t = Template( """ <%block name="foo" cached="True" cache_key="${mykey}"> some block </%block> """ ) m = self._install_mock_cache(t) t.render(mykey="thekey") t.render(mykey="thekey") eq_(result_lines(t.render(mykey="thekey")), ["some block"]) eq_(m.key, "thekey") t = Template( """ <%def name="foo()" cached="True" cache_key="${mykey}"> some def </%def> ${foo()} """ ) m = self._install_mock_cache(t) t.render(mykey="thekey") t.render(mykey="thekey") eq_(result_lines(t.render(mykey="thekey")), ["some def"]) eq_(m.key, "thekey")
def test_interpret_expression_from_arg_two(self): """test that cache_key=${foo} gets its value from the 'foo' argument regardless of it being passed from the context. This is here testing that there's no change to existing behavior before and after #191. """ t = Template(""" <%def name="layout(foo)" cached="True" cache_key="${foo}"> foo: ${value} </%def> ${layout(3)} """, cache_impl="plain") eq_( result_lines(t.render(foo='foo', value=1)), ["foo: 1"] ) eq_( result_lines(t.render(foo='bar', value=2)), ["foo: 1"] )
def test_module_roundtrip(self): lookup = TemplateLookup() template = Template(""" <%inherit file="base.html"/> % for x in range(5): ${x} % endfor """, lookup=lookup) base = Template(""" This is base. ${self.body()} """, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines(template.render()) == [ "This is base.", "0", "1", "2", "3", "4" ] lookup = TemplateLookup() template = ModuleTemplate(template.module, lookup=lookup) base = ModuleTemplate(base.module, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines(template.render()) == [ "This is base.", "0", "1", "2", "3", "4" ]
def test_render(self): assert result_lines(tl.render({}, template='/index.html')) == [ "this is index" ] assert result_lines(tl.render({}, template=compat.u('/index.html'))) == [ "this is index" ]
def test_undefined(self): t = Template(""" % if x is UNDEFINED: undefined % else: x: ${x} % endif """) assert result_lines(t.render(x=12)) == ["x: 12"] assert result_lines(t.render(y=12)) == ["undefined"]
def test_lookup(self): l = TemplateLookup(cache_impl='mock') l.put_string("x", """ <%page cached="True" /> ${y} """) t = l.get_template("x") self._install_mock_cache(t) assert result_lines(t.render(y=5)) == ["5"] assert result_lines(t.render(y=7)) == ["5"] assert isinstance(t.cache.impl, MockCacheImpl)
def test_preprocess2(self): t = """ %call foo(): hi %endcall """ s = """ <%call expr="foo()"> hi </%call> """ t = convert_calls(t) assert_equals(result_lines(t), result_lines(s))
def test_preprocess(self): t = """ % def foo(): hi % enddef """ s = """ <%def name="foo()"> hi </%def> """ t = convert_defs(t) assert_equals(result_lines(t), result_lines(s))
def test_invalidate(self): t = Template(""" <%%def name="foo()" cached="True"> foo: ${x} </%%def> <%%def name="bar()" cached="True" cache_type='dbm' cache_dir='%s'> bar: ${x} </%%def> ${foo()} ${bar()} """ % module_base) self._install_mock_cache(t) assert result_lines(t.render(x=1)) == ["foo: 1", "bar: 1"] assert result_lines(t.render(x=2)) == ["foo: 1", "bar: 1"] t.cache.invalidate_def('foo') assert result_lines(t.render(x=3)) == ["foo: 3", "bar: 1"] t.cache.invalidate_def('bar') assert result_lines(t.render(x=4)) == ["foo: 3", "bar: 4"] t = Template(""" <%%page cached="True" cache_type="dbm" cache_dir="%s"/> page: ${x} """ % module_base) self._install_mock_cache(t) assert result_lines(t.render(x=1)) == ["page: 1"] assert result_lines(t.render(x=2)) == ["page: 1"] t.cache.invalidate_body() assert result_lines(t.render(x=3)) == ["page: 3"] assert result_lines(t.render(x=4)) == ["page: 3"]
def test_chained_call_in_nested(self): t = Template(""" <%def name="embedded()"> <%def name="a()"> this is a. <%call expr="b()"> this is a's ccall. heres my body: ${caller.body()} </%call> </%def> <%def name="b()"> this is b. heres my body: ${caller.body()} whats in the body's caller's body ? ${context.caller_stack[-2].body()} </%def> <%call expr="a()"> heres the main templ call </%call> </%def> ${embedded()} """) #print t.code #print result_lines(t.render()) assert result_lines(t.render()) == [ 'this is a.', 'this is b. heres my body:', "this is a's ccall. heres my body:", 'heres the main templ call', "whats in the body's caller's body ?", 'heres the main templ call' ]
def test_fileargs_lookup(self): l = lookup.TemplateLookup(cache_dir=module_base, cache_type="file") l.put_string( "test", """ <%! callcount = [0] %> <%def name="foo()" cached="True"> this is foo <% callcount[0] += 1 %> </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """, ) t = l.get_template("test") m = self._install_mock_cache(t) assert result_lines(l.get_template("test").render()) == [ "this is foo", "this is foo", "this is foo", "callcount: [1]", ] eq_(m.kwargs, {"dir": module_base, "type": "file"})
def test_ccall_caller(self): t = Template(""" <%def name="outer_func()"> OUTER BEGIN <%call expr="caller.inner_func()"> INNER CALL </%call> OUTER END </%def> <%call expr="outer_func()"> <%def name="inner_func()"> INNER BEGIN ${caller.body()} INNER END </%def> </%call> """) #print t.code assert result_lines(t.render()) == [ "OUTER BEGIN", "INNER BEGIN", "INNER CALL", "INNER END", "OUTER END", ]
def test_stack_pop(self): t = Template(""" <%def name="links()" buffered="True"> Some links </%def> <%def name="wrapper(links)"> <h1>${caller.body()}</h1> ${links} </%def> ## links() pushes a stack frame on. when complete, ## 'nextcaller' must be restored <%call expr="wrapper(links())"> Some title </%call> """) assert result_lines(t.render()) == [ "<h1>", "Some title", "</h1>", "Some links" ]
def test_includes(self): """test that an included template also has its full hierarchy invoked.""" collection = lookup.TemplateLookup() collection.put_string("base", """ <%def name="a()">base_a</%def> This is the base. ${next.body()} End base. """) collection.put_string("index",""" <%inherit file="base"/> this is index. a is: ${self.a()} <%include file="secondary"/> """) collection.put_string("secondary",""" <%inherit file="base"/> this is secondary. a is: ${self.a()} """) assert result_lines(collection.get_template("index").render()) == [ 'This is the base.', 'this is index.', 'a is: base_a', 'This is the base.', 'this is secondary.', 'a is: base_a', 'End base.', 'End base.' ]
def test_basic(self): t = Template(""" <%! cached = None %> <%def name="foo()"> <% global cached if cached: return "cached: " + cached __M_writer = context._push_writer() %> this is foo <% buf, __M_writer = context._pop_buffer_and_writer() cached = buf.getvalue() return cached %> </%def> ${foo()} ${foo()} """) assert result_lines(t.render()) == [ "this is foo", "cached:", "this is foo" ]
def test_ui(self): t = tl2.get_template("ui.html") assert result_lines(t.render()) == [ "Test ui", "This is a post named 'post name'" ]
def test_new_syntax(self): """test foo:bar syntax, including multiline args and expression eval.""" # note the trailing whitespace in the bottom ${} expr, need to strip # that off < python 2.7 t = Template(""" <%def name="foo(x, y, q, z)"> ${x} ${y} ${q} ${",".join("%s->%s" % (a, b) for a, b in z)} </%def> <%self:foo x="this is x" y="${'some ' + 'y'}" q=" this is q" z="${[ (1, 2), (3, 4), (5, 6) ] }"/> """) eq_( result_lines(t.render()), ['this is x', 'some y', 'this', 'is', 'q', '1->2,3->4,5->6'] )
def test_call_in_nested_2(self): t = Template(""" <%def name="a()"> <%def name="d()"> not this d </%def> this is a ${b()} <%def name="b()"> <%def name="d()"> not this d either </%def> this is b <%call expr="c()"> <%def name="d()"> this is d </%def> this is the body in b's call </%call> </%def> <%def name="c()"> this is c: ${caller.body()} the embedded "d" is: ${caller.d()} </%def> </%def> ${a()} """) assert result_lines(t.render()) == ['this is a', 'this is b', 'this is c:', "this is the body in b's call", 'the embedded "d" is:', 'this is d']
def test_nested_call_2(self): t = Template(""" x is ${x} <%def name="foo()"> ${caller.foosub(x=10)} </%def> <%def name="bar()"> bar: ${caller.barsub()} </%def> <%call expr="foo()"> <%def name="foosub(x)"> this is foo body: ${x} <%call expr="bar()"> <%def name="barsub()"> this is bar body: ${x} </%def> </%call> </%def> </%call> """) assert result_lines(t.render(x=5)) == [ "x is 5", "this is foo body: 10", "bar:", "this is bar body: 10" ]
def test_nested_call(self): """test %calls that are nested inside each other""" t = Template(""" <%def name="foo()"> ${caller.body(x=10)} </%def> x is ${x} <%def name="bar()"> bar: ${caller.body()} </%def> <%call expr="foo()" args="x"> this is foo body: ${x} <%call expr="bar()"> this is bar body: ${x} </%call> </%call> """) assert result_lines(t.render(x=5)) == [ "x is 5", "this is foo body: 10", "bar:", "this is bar body: 10" ]
def test_fileargs_implicit(self): l = lookup.TemplateLookup(module_directory=module_base) l.put_string( "test", """ <%! callcount = [0] %> <%def name="foo()" cached="True" cache_type='dbm'> this is foo <% callcount[0] += 1 %> </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """, ) m = self._install_mock_cache(l.get_template("test")) assert result_lines(l.get_template("test").render()) == [ "this is foo", "this is foo", "this is foo", "callcount: [1]", ] eq_(m.kwargs, {"type": "dbm"})
def test_compound_call(self): t = Template(""" <%def name="bar()"> this is bar </%def> <%def name="comp1()"> this comp1 should not be called </%def> <%def name="foo()"> foo calling comp1: ${caller.comp1(x=5)} foo calling body: ${caller.body()} </%def> <%call expr="foo()"> <%def name="comp1(x)"> this is comp1, ${x} </%def> this is the body, ${comp1(6)} </%call> ${bar()} """) assert result_lines(t.render()) == ['foo calling comp1:', 'this is comp1, 5', 'foo calling body:', 'this is the body,', 'this is comp1, 6', 'this is bar']
def test_updir(self): t = tl.get_template('/subdir/foo/../bar/../index.html') assert result_lines(t.render()) == [ "this is sub index", "this is include 2" ]
def test_chained_call(self): """test %calls that are chained through their targets""" t = Template(""" <%def name="a()"> this is a. <%call expr="b()"> this is a's ccall. heres my body: ${caller.body()} </%call> </%def> <%def name="b()"> this is b. heres my body: ${caller.body()} whats in the body's caller's body ? ${context.caller_stack[-2].body()} </%def> <%call expr="a()"> heres the main templ call </%call> """) assert result_lines(t.render()) == [ 'this is a.', 'this is b. heres my body:', "this is a's ccall. heres my body:", 'heres the main templ call', "whats in the body's caller's body ?", 'heres the main templ call' ]
def test_nested_def(self): t = Template( """ <%! callcount = [0] %> <%def name="foo()"> <%def name="bar()" cached="True"> this is foo <% callcount[0] += 1 %> </%def> ${bar()} </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """ ) m = self._install_mock_cache(t) assert result_lines(t.render()) == ["this is foo", "this is foo", "this is foo", "callcount: [1]"] assert m.kwargs == {}
def test_pageargs_2(self): collection = lookup.TemplateLookup() collection.put_string("base", """ this is the base. ${next.body(**context.kwargs)} <%def name="foo(**kwargs)"> ${next.body(**kwargs)} </%def> <%def name="bar(**otherargs)"> ${next.body(z=16, **context.kwargs)} </%def> ${foo(x=12, y=15, z=8)} ${bar(x=19, y=17)} """) collection.put_string("index", """ <%inherit file="base"/> <%page args="x, y, z=7"/> pageargs: ${x}, ${y}, ${z} """) assert result_lines(collection.get_template('index').render(x=5,y=10)) == [ "this is the base.", "pageargs: 5, 10, 7", "pageargs: 12, 15, 8", "pageargs: 5, 10, 16" ]
def test_scope_eleven(self): t = Template(""" x is ${x} <%def name="a(x)"> this is a, ${b()} <%def name="b()"> this is b, x is ${x} </%def> </%def> ${a(x=5)} """) eq_( result_lines(t.render(x=10)), [ "x is 10", "this is a,", "this is b, x is 5" ])
def test_ccall_2(self): collection = lookup.TemplateLookup() collection.put_string( "base.html", """ <%namespace name="foo" file="ns1.html" inheritable="True"/> ${next.body()} """) collection.put_string( "ns1.html", """ <%namespace name="foo2" file="ns2.html"/> <%def name="bar()"> <%call expr="foo2.ns2_bar()"> this is ns1.html->bar caller body: ${caller.body()} </%call> </%def> """) collection.put_string( "ns2.html", """ <%def name="ns2_bar()"> this is ns2.html->bar caller body: ${caller.body()} </%def> """) collection.put_string( "index.html", """ <%inherit file="base.html"/> this is index <%call expr="self.foo.bar()"> call body </%call> """) assert result_lines( collection.get_template("index.html").render()) == [ "this is index", "this is ns2.html->bar", "caller body:", "this is ns1.html->bar", "caller body:", "call body" ]
def test_in_call(self): collection = lookup.TemplateLookup() collection.put_string( "/layout.html", """ Super layout! <%call expr="self.grid()"> ${next.body()} </%call> Oh yea! <%def name="grid()"> Parent grid ${caller.body()} End Parent </%def> """) collection.put_string( "/subdir/layout.html", """ ${next.body()} <%def name="grid()"> Subdir grid ${caller.body()} End subdir </%def> <%inherit file="/layout.html"/> """) collection.put_string( "/subdir/renderedtemplate.html", """ Holy smokes! <%inherit file="/subdir/layout.html"/> """) #print collection.get_template("/layout.html").code #print collection.get_template("/subdir/renderedtemplate.html").render() assert result_lines( collection.get_template( "/subdir/renderedtemplate.html").render()) == [ "Super layout!", "Subdir grid", "Holy smokes!", "End subdir", "Oh yea!" ]
def test_page(self): t = Template(""" <%! callcount = [0] %> <%page cached="True"/> this is foo <% callcount[0] += 1 %> callcount: ${callcount} """) m = self._install_mock_cache(t) t.render() t.render() assert result_lines(t.render()) == [ "this is foo", "callcount: [1]" ] assert m.kwargs == {}
def test_buffered(self): t = Template( """ <%! def a(text): return "this is a " + text.strip() %> ${foo()} ${foo()} <%def name="foo()" cached="True" buffered="True"> this is a test </%def> """, buffer_filters=["a"], ) self._install_mock_cache(t) eq_( result_lines(t.render()), ["this is a this is a test", "this is a this is a test"], )
def test_ccall_import(self): collection = lookup.TemplateLookup() collection.put_string( "functions.html", """ <%def name="foo()"> this is foo </%def> <%def name="bar()"> this is bar. ${caller.body()} ${caller.lala()} </%def> """, ) collection.put_string( "index.html", """ <%namespace name="func" file="functions.html" import="*"/> <%call expr="bar()"> this is index embedded foo is ${foo()} <%def name="lala()"> this is lala ${foo()} </%def> </%call> """, ) # print collection.get_template("index.html").code # print collection.get_template("functions.html").code assert result_lines( collection.get_template("index.html").render()) == [ "this is bar.", "this is index embedded", "foo is", "this is foo", "this is lala", "this is foo", ]
def test_format_exceptions_no_pygments(self): l = TemplateLookup(format_exceptions=True) l.put_string( "foo.html", """ <%inherit file="base.html"/> ${foobar} """, ) l.put_string( "base.html", """ ${self.body()} """, ) assert '<div class="sourceline">${foobar}</div>' in result_lines( l.get_template("foo.html").render_unicode() )
def test_expr_grouping(self): """test that parenthesis are placed around string-embedded expressions.""" template = Template( """ <%def name="bar(x, y)"> ${x} ${y} </%def> <%self:bar x=" ${foo} " y="x${g and '1' or '2'}y"/> """, input_encoding="utf-8", ) # the concat has to come out as "x + (g and '1' or '2') + y" assert result_lines(template.render(foo="this is foo", g=False)) == [ "this is foo", "x2y", ]
def test_inline_expression_from_arg_one(self): """test that cache_key=${foo} gets its value from the 'foo' argument in the <%def> tag, and strict_undefined doesn't complain. this is #191. """ t = Template( """ <%def name="layout(foo)" cached="True" cache_key="${foo}"> foo: ${foo} </%def> ${layout(3)} """, strict_undefined=True, cache_impl="plain", ) eq_(result_lines(t.render()), ["foo: 3"])
def test_custom_tag_3(self): collection = lookup.TemplateLookup() collection.put_string( "base.html", """ <%namespace name="foo" file="ns.html" inheritable="True"/> ${next.body()} """, ) collection.put_string( "ns.html", """ <%def name="bar()"> this is ns.html->bar caller body: ${caller.body()} </%def> """, ) collection.put_string( "index.html", """ <%inherit file="base.html"/> this is index <%self.foo:bar> call body </%self.foo:bar> """, ) assert result_lines( collection.get_template("index.html").render()) == [ "this is index", "this is ns.html->bar", "caller body:", "call body", ]
def test_in_def(self): collection = lookup.TemplateLookup() collection.put_string( "main.html", """ <%namespace name="foo" file="ns.html"/> this is main. ${bar()} <%def name="bar()"> this is bar, foo is ${foo.bar()} </%def> """) collection.put_string( "ns.html", """ <%def name="bar()"> this is ns.html->bar </%def> """) assert result_lines(collection.get_template("main.html").render()) == [ "this is main.", "this is bar, foo is", "this is ns.html->bar" ]
def test_inheritance_two(self): collection = lookup.TemplateLookup() collection.put_string( "base.html", """ <%def name="foo()"> base.foo </%def> <%def name="bat()"> base.bat </%def> """) collection.put_string( "lib.html", """ <%inherit file="base.html"/> <%def name="bar()"> lib.bar ${parent.foo()} ${self.foo()} ${parent.bat()} ${self.bat()} </%def> <%def name="foo()"> lib.foo </%def> """) collection.put_string( "front.html", """ <%namespace name="lib" file="lib.html"/> ${lib.bar()} """) assert result_lines( collection.get_template("front.html").render()) == [ 'lib.bar', 'base.foo', 'lib.foo', 'base.bat', 'base.bat' ]
def test_control(self): t = Template(""" ## this is a template. % for x in y: % if 'test' in x: yes x has test % else: no x does not have test %endif %endfor """) assert result_lines( t.render(y=[{ 'test': 'one' }, { 'foo': 'bar' }, { 'foo': 'bar', 'test': 'two' }])) == [ "yes x has test", "no x does not have test", "yes x has test" ]
def test_call_in_nested(self): t = Template(""" <%def name="a()"> this is a ${b()} <%def name="b()"> this is b <%call expr="c()"> this is the body in b's call </%call> </%def> <%def name="c()"> this is c: ${caller.body()} </%def> </%def> ${a()} """) assert result_lines(t.render()) == [ "this is a", "this is b", "this is c:", "this is the body in b's call", ]
def test_invalidate(self): t = Template( """ <%%def name="foo()" cached="True"> foo: ${x} </%%def> <%%def name="bar()" cached="True" cache_type='dbm' cache_dir='%s'> bar: ${x} </%%def> ${foo()} ${bar()} """ % module_base ) self._install_mock_cache(t) assert result_lines(t.render(x=1)) == ["foo: 1", "bar: 1"] assert result_lines(t.render(x=2)) == ["foo: 1", "bar: 1"] t.cache.invalidate_def("foo") assert result_lines(t.render(x=3)) == ["foo: 3", "bar: 1"] t.cache.invalidate_def("bar") assert result_lines(t.render(x=4)) == ["foo: 3", "bar: 4"] t = Template( """ <%%page cached="True" cache_type="dbm" cache_dir="%s"/> page: ${x} """ % module_base ) self._install_mock_cache(t) assert result_lines(t.render(x=1)) == ["page: 1"] assert result_lines(t.render(x=2)) == ["page: 1"] t.cache.invalidate_body() assert result_lines(t.render(x=3)) == ["page: 3"] assert result_lines(t.render(x=4)) == ["page: 3"]
def test_conditional_call(self): """test that 'caller' is non-None only if the immediate <%def> was called via <%call>""" t = Template(""" <%def name="a()"> % if caller: ${ caller.body() } \\ % endif AAA ${ b() } </%def> <%def name="b()"> % if caller: ${ caller.body() } \\ % endif BBB ${ c() } </%def> <%def name="c()"> % if caller: ${ caller.body() } \\ % endif CCC </%def> <%call expr="a()"> CALL </%call> """) assert result_lines(t.render()) == [ "CALL", "AAA", "BBB", "CCC" ]
def test_dynamic_key_with_imports(self): lookup = TemplateLookup() lookup.put_string( "foo.html", """ <%! callcount = [0] %> <%namespace file="ns.html" import="*"/> <%page cached="True" cache_key="${foo}"/> this is foo <% callcount[0] += 1 %> callcount: ${callcount} """) lookup.put_string("ns.html", """""") t = lookup.get_template("foo.html") m = self._install_mock_cache(t) t.render(foo='somekey') t.render(foo='somekey') assert result_lines( t.render(foo='somekey')) == ["this is foo", "callcount: [1]"] assert m.kwargs == {}
def test_basic(self): collection = lookup.TemplateLookup() collection.put_string('main', """ <%inherit file="base"/> <%def name="header()"> main header. </%def> this is the content. """) collection.put_string('base', """ This is base. header: ${self.header()} body: ${self.body()} footer: ${self.footer()} <%def name="footer()"> this is the footer. header again ${next.header()} </%def> """) assert result_lines(collection.get_template('main').render()) == [ 'This is base.', 'header:', 'main header.', 'body:', 'this is the content.', 'footer:', 'this is the footer. header again', 'main header.' ]
def test_custom_tag_2(self): collection = lookup.TemplateLookup() collection.put_string( "base.html", """ <%def name="foo(x, y)"> foo: ${x} ${y} </%def> <%def name="bat(g)"><% return "the bat! %s" % g %></%def> <%def name="bar(x)"> ${caller.body(z=x)} </%def> """, ) collection.put_string( "index.html", """ <%namespace name="myns" file="base.html"/> <%myns:foo x="${'some x'}" y="some y"/> <%myns:bar x="${myns.bat(10)}" args="z"> record: ${z} </%myns:bar> """, ) assert result_lines( collection.get_template("index.html").render()) == [ "foo: some x some y", "record: the bat! 10" ]
def test_pageargs_2(self): collection = lookup.TemplateLookup() collection.put_string( "base", """ this is the base. ${next.body(**context.kwargs)} <%def name="foo(**kwargs)"> ${next.body(**kwargs)} </%def> <%def name="bar(**otherargs)"> ${next.body(z=16, **context.kwargs)} </%def> ${foo(x=12, y=15, z=8)} ${bar(x=19, y=17)} """, ) collection.put_string( "index", """ <%inherit file="base"/> <%page args="x, y, z=7"/> pageargs: ${x}, ${y}, ${z} """, ) assert result_lines( collection.get_template("index").render(x=5, y=10) ) == [ "this is the base.", "pageargs: 5, 10, 7", "pageargs: 12, 15, 8", "pageargs: 5, 10, 16", ]
def test_basic_dot(self): t = tl.load_template("index") assert result_lines(t.render()) == ["this is index"]
def test_future_import(self): t = Template("${ x / y }", future_imports=["division"]) assert result_lines(t.render(x=12, y=5)) == ["2.4"]
def _test_custom_lexer(self, template): eq_( result_lines(template.render()), ["hello world"] )
def test_namespaces(self): """test that templates used via <%namespace> have access to an inheriting 'self', and that the full 'self' is also exported.""" collection = lookup.TemplateLookup() collection.put_string( "base", """ <%def name="a()">base_a</%def> <%def name="b()">base_b</%def> This is the base. ${next.body()} """, ) collection.put_string( "layout", """ <%inherit file="base"/> <%def name="a()">layout_a</%def> This is the layout.. ${next.body()} """, ) collection.put_string( "index", """ <%inherit file="base"/> <%namespace name="sc" file="secondary"/> this is index. a is: ${self.a()} sc.a is: ${sc.a()} sc.b is: ${sc.b()} sc.c is: ${sc.c()} sc.body is: ${sc.body()} """, ) collection.put_string( "secondary", """ <%inherit file="layout"/> <%def name="c()">secondary_c. a is ${self.a()} b is ${self.b()} """ """d is ${self.d()}</%def> <%def name="d()">secondary_d.</%def> this is secondary. a is: ${self.a()} c is: ${self.c()} """, ) assert result_lines(collection.get_template("index").render()) == [ "This is the base.", "this is index.", "a is: base_a", "sc.a is: layout_a", "sc.b is: base_b", "sc.c is: secondary_c. a is layout_a b is base_b d is " "secondary_d.", "sc.body is:", "this is secondary.", "a is: layout_a", "c is: secondary_c. a is layout_a b is base_b d is secondary_d.", ]
def test_multilevel_nesting(self): collection = lookup.TemplateLookup() collection.put_string( "main", """ <%inherit file="layout"/> <%def name="d()">main_d</%def> main_body ${parent.d()} full stack from the top: ${self.name} ${parent.name} ${parent.context['parent'].name} """ """${parent.context['parent'].context['parent'].name} """, ) collection.put_string( "layout", """ <%inherit file="general"/> <%def name="d()">layout_d</%def> layout_body parent name: ${parent.name} ${parent.d()} ${parent.context['parent'].d()} ${next.body()} """, ) collection.put_string( "general", """ <%inherit file="base"/> <%def name="d()">general_d</%def> general_body ${next.d()} ${next.context['next'].d()} ${next.body()} """, ) collection.put_string( "base", """ base_body full stack from the base: ${self.name} ${self.context['parent'].name} """ """${self.context['parent'].context['parent'].name} """ """${self.context['parent'].context['parent'].context['parent'].name} ${next.body()} <%def name="d()">base_d</%def> """, ) assert result_lines(collection.get_template("main").render()) == [ "base_body", "full stack from the base:", "self:main self:layout self:general self:base", "general_body", "layout_d", "main_d", "layout_body", "parent name: self:general", "general_d", "base_d", "main_body layout_d", "full stack from the top:", "self:main self:layout self:general self:base", ]
def test_updir(self): t = tl.get_template("/subdir/foo/../bar/../index.html") assert result_lines(t.render()) == [ "this is sub index", "this is include 2", ]
def test_basic(self): t = tl.load_template("/index.html") assert result_lines(t.render()) == ["this is index"]
def test_render(self): assert result_lines(tl.render( {}, template="/index.html")) == ["this is index"] assert result_lines(tl.render( {}, template=compat.u("/index.html"))) == ["this is index"]
def test_basic(self): t = tl.get_template('index.html') assert result_lines(t.render()) == [ "this is index" ]