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_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_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 util.py3k: l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""") else: l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""") if util.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(self): collection = lookup.TemplateLookup() collection.put_string( "base", """ this is the base. ${next.body()} """) collection.put_string( "index", """ <%! def dyn(context): if context.get('base', None) is not None: return 'base' else: return None %> <%inherit file="${dyn(context)}"/> this is index. """) assert result_lines( collection.get_template('index').render()) == ['this is index.'] assert result_lines( collection.get_template('index').render(base=True)) == [ 'this is the base.', 'this is index.' ]
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 util.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_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_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_lookup(self): l = TemplateLookup(cache_impl='mock') l.put_string("x", """ <%page cached="True" /> ${y} """) t = l.get_template("x") assert result_lines(t.render(y=5)) == ["5"] assert result_lines(t.render(y=7)) == ["5"] assert isinstance(t.cache.impl, MockCacheImpl)
def test_lookup(self): l = TemplateLookup(cache_impl='mock') l.put_string( "x", """ <%page cached="True" /> ${y} """) t = l.get_template("x") assert result_lines(t.render(y=5)) == ["5"] assert result_lines(t.render(y=7)) == ["5"] assert isinstance(t.cache.impl, MockCacheImpl)
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_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) 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) 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_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_inheritance(self): """test namespace initialization in a base inherited template that doesnt otherwise access the namespace""" 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 </%def> """) collection.put_string("index.html", """ <%inherit file="base.html"/> this is index ${self.foo.bar()} """) assert result_lines(collection.get_template("index.html").render()) == [ "this is index", "this is ns.html->bar" ]
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_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_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_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_closure_import(self): collection = lookup.TemplateLookup() collection.put_string("functions.html",""" <%def name="foo()"> this is foo </%def> <%def name="bar()"> this is bar </%def> """) collection.put_string("index.html", """ <%namespace file="functions.html" import="*"/> <%def name="cl1()"> ${foo()} </%def> <%def name="cl2()"> ${bar()} </%def> ${cl1()} ${cl2()} """) assert result_lines(collection.get_template("index.html").render(bar="this is bar", x="this is x")) == [ "this is foo", "this is bar", ]
def test_ccall(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 <%call expr="self.foo.bar()"> call body </%call> """) assert result_lines(collection.get_template("index.html").render()) == [ "this is index", "this is ns.html->bar", "caller body:", "call body" ]
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_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_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_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_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_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_fileargs_lookup(self): l = lookup.TemplateLookup(cache_dir='./test_htdocs', 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]', ] assert m.kwargs == {'data_dir':'./test_htdocs', 'type':'file'}
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_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(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_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_in_remote_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> """) collection.put_string("index.html", """ <%namespace name="main" file="main.html"/> this is index ${main.bar()} """) assert result_lines(collection.get_template("index.html").render()) == [ "this is index", "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_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_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_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_fileargs_pagetag(self): t = Template(""" <%page cache_dir='./test_htdocs' cache_type='dbm'/> <%! callcount = [0] %> <%def name="foo()" cached="True"> this is foo <% callcount[0] += 1 %> </%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 == {'data_dir':'./test_htdocs', 'type':'dbm'}
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_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_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_fileargs_pagetag(self): t = Template(""" <%%page cache_dir='%s' cache_type='dbm'/> <%%! callcount = [0] %%> <%%def name="foo()" cached="True"> this is foo <%% callcount[0] += 1 %%> </%%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """ % module_base) 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 == {'data_dir': module_base, 'type': 'dbm'}
def test_subdir(self): t = tl.get_template('/subdir/index.html') assert result_lines( t.render()) == ["this is sub index", "this is include 2"] assert tl.get_template('/subdir/index.html').module_id \ == '_subdir_index_html'
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]', ] assert m.kwargs == {'data_dir': module_base, 'type': 'file'}