コード例 #1
0
    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'
            ]
コード例 #2
0
 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.'
         ]
コード例 #3
0
    def test_overload(self):
        collection = lookup.TemplateLookup()

        collection.put_string(
            'main.html', """
        <%namespace name="comp" file="defs.html">
            <%def name="def1(x, y)">
                overridden def1 ${x}, ${y}
            </%def>
        </%namespace>

        this is main.  ${comp.def1("hi", "there")}
        ${comp.def2("there")}
    """)

        collection.put_string(
            'defs.html', """
        <%def name="def1(s)">
            def1: ${s}
        </%def>

        <%def name="def2(x)">
            def2: ${x}
        </%def>
    """)

        assert flatten_result(collection.get_template('main.html').render(
        )) == "this is main. overridden def1 hi, there def2: there"
コード例 #4
0
    def test_context(self):
        """test that namespace callables get access to the current context"""
        collection = lookup.TemplateLookup()

        collection.put_string(
            'main.html', """
        <%namespace name="comp" file="defs.html"/>

        this is main.  ${comp.def1()}
        ${comp.def2("there")}
""")

        collection.put_string(
            'defs.html', """
        <%def name="def1()">
            def1: x is ${x}
        </%def>

        <%def name="def2(x)">
            def2: x is ${x}
        </%def>
""")

        assert flatten_result(
            collection.get_template('main.html').render(x="context x")
        ) == "this is main. def1: x is context x def2: x is there"
コード例 #5
0
    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"
            ]
コード例 #6
0
ファイル: test_cache.py プロジェクト: whiteclover/Choco
    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'})
コード例 #7
0
ファイル: test_inheritance.py プロジェクト: whiteclover/Choco
    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"
            ]
コード例 #8
0
ファイル: test_cache.py プロジェクト: whiteclover/Choco
    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'})
コード例 #9
0
    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"
            ]
コード例 #10
0
    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.'
        ]
コード例 #11
0
    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.'
        ]
コード例 #12
0
    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"
            ]
コード例 #13
0
    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"
            ]
コード例 #14
0
    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"
            ]
コード例 #15
0
    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",
                                                         ]
コード例 #16
0
    def test_attr(self):
        l = lookup.TemplateLookup()

        l.put_string(
            "foo.html", """
        <%!
            foofoo = "foo foo"
            onlyfoo = "only foo"
        %>
        <%inherit file="base.html"/>
        <%def name="setup()">
            <%
            self.attr.foolala = "foo lala"
            %>
        </%def>
        ${self.attr.basefoo}
        ${self.attr.foofoo}
        ${self.attr.onlyfoo}
        ${self.attr.lala}
        ${self.attr.foolala}
        """)

        l.put_string(
            "base.html", """
        <%!
            basefoo = "base foo 1"
            foofoo = "base foo 2"
        %>
        <%
            self.attr.lala = "base lala"
        %>

        ${self.attr.basefoo}
        ${self.attr.foofoo}
        ${self.attr.onlyfoo}
        ${self.attr.lala}
        ${self.setup()}
        ${self.attr.foolala}
        body
        ${self.body()}
        """)

        assert result_lines(l.get_template("foo.html").render()) == [
            "base foo 1",
            "foo foo",
            "only foo",
            "base lala",
            "foo lala",
            "body",
            "base foo 1",
            "foo foo",
            "only foo",
            "base lala",
            "foo lala",
        ]
コード例 #17
0
ファイル: test_inheritance.py プロジェクト: whiteclover/Choco
    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'
        ]
コード例 #18
0
    def test_module_imports_2(self):
        collection = lookup.TemplateLookup()

        collection.put_string(
            'main.html', """
        <%namespace import="foo1, foo2" module="test.foo.test_ns"/>

        this is main.  ${foo1()}
        ${foo2("hi")}
""")

        assert flatten_result(collection.get_template('main.html').render(
        )) == "this is main. this is foo1. this is foo2, x is hi"
コード例 #19
0
    def test_module(self):
        collection = lookup.TemplateLookup()

        collection.put_string(
            'main.html', """
        <%namespace name="comp" module="test.sample_module_namespace"/>

        this is main.  ${comp.foo1()}
        ${comp.foo2("hi")}
""")

        assert flatten_result(collection.get_template('main.html').render(
        )) == "this is main. this is foo1. this is foo2, x is hi"
コード例 #20
0
    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.'
        ]
コード例 #21
0
ファイル: test_inheritance.py プロジェクト: whiteclover/Choco
 def test_pageargs_err(self):
     collection = lookup.TemplateLookup()
     collection.put_string("base", """
         this is the base.
         ${next.body()}
     """)
     collection.put_string("index", """
         <%inherit file="base"/>
         <%page args="x, y, z=7"/>
         print ${x}, ${y}, ${z}
     """)
     try:
         print(collection.get_template('index').render(x=5,y=10))
         assert False
     except TypeError:
         assert True
コード例 #22
0
    def test_dynamic(self):
        collection = lookup.TemplateLookup()

        collection.put_string(
            'a', """
        <%namespace name="b" file="${context['b_def']}"/>

        a.  b: ${b.body()}
""")

        collection.put_string('b', """
        b.
""")

        eq_(flatten_result(collection.get_template('a').render(b_def='b')),
            "a. b: b.")
コード例 #23
0
    def test_attr_raise(self):
        l = lookup.TemplateLookup()

        l.put_string(
            "foo.html", """
            <%def name="foo()">
            </%def>
        """)

        l.put_string(
            "bar.html", """
        <%namespace name="foo" file="foo.html"/>

        ${foo.notfoo()}
        """)

        self.assertRaises(AttributeError, l.get_template("bar.html").render)
コード例 #24
0
ファイル: test_inheritance.py プロジェクト: whiteclover/Choco
 def test_toplevel(self):
     collection = lookup.TemplateLookup()
     collection.put_string("base", """
         this is the base.
         ${next.body()}
     """)
     collection.put_string("index", """
         <%inherit file="base"/>
         this is the body
     """)
     assert result_lines(collection.get_template('index').render()) == [
         "this is the base.",
         "this is the body"
     ]
     assert result_lines(collection.get_template('index').get_def("body").render()) == [
         "this is the body"
     ]
コード例 #25
0
 def test_getattr(self):
     collection = lookup.TemplateLookup()
     collection.put_string(
         "main.html", """
         <%namespace name="foo" file="ns.html"/>
         <%
              if hasattr(foo, 'lala'):
                  foo.lala()
              if not hasattr(foo, 'hoho'):
                  context.write('foo has no hoho.')
         %>
      """)
     collection.put_string(
         "ns.html", """
       <%def name="lala()">this is lala.</%def>
     """)
     assert flatten_result(collection.get_template(
         "main.html").render()) == "this is lala.foo has no hoho."
コード例 #26
0
    def test_dont_pollute_self(self):
        # test that get_namespace() doesn't modify the original context
        # incompatibly

        collection = lookup.TemplateLookup()
        collection.put_string(
            "base.html", """

        <%def name="foo()">
        <%
            foo = local.get_namespace("foo.html")
        %>
        </%def>

        name: ${self.name}
        name via bar: ${bar()}

        ${next.body()}

        name: ${self.name}
        name via bar: ${bar()}
        <%def name="bar()">
            ${self.name}
        </%def>


        """)

        collection.put_string(
            "page.html", """
        <%inherit file="base.html"/>

        ${self.foo()}

        hello world

        """)

        collection.put_string("foo.html", """<%inherit file="base.html"/>""")
        assert result_lines(collection.get_template("page.html").render()) == [
            "name: self:page.html", "name via bar:", "self:page.html",
            "hello world", "name: self:page.html", "name via bar:",
            "self:page.html"
        ]
コード例 #27
0
    def test_import(self):
        collection = lookup.TemplateLookup()
        collection.put_string(
            "functions.html", """
            <%def name="foo()">
                this is foo
            </%def>

            <%def name="bar()">
                this is bar
            </%def>

            <%def name="lala()">
                this is lala
            </%def>
        """)

        collection.put_string(
            "func2.html", """
            <%def name="a()">
                this is a
            </%def>
            <%def name="b()">
                this is b
            </%def>
        """)
        collection.put_string(
            "index.html", """
            <%namespace file="functions.html" import="*"/>
            <%namespace file="func2.html" import="a, b"/>
            ${foo()}
            ${bar()}
            ${lala()}
            ${a()}
            ${b()}
            ${x}
        """)

        assert result_lines(
            collection.get_template("index.html").render(
                bar="this is bar", x="this is x")) == [
                    "this is foo", "this is bar", "this is lala", "this is a",
                    "this is b", "this is x"
                ]
コード例 #28
0
ファイル: test_def.py プロジェクト: Python3pkg/Choco
    def test_scope_nine(self):
        """test that 'enclosing scope' doesnt
        get exported to other templates"""

        l = lookup.TemplateLookup()
        l.put_string(
            'main', """
        <%
            x = 5
        %>
        this is main.  <%include file="secondary"/>
""")

        l.put_string('secondary', """
        this is secondary.  x is ${x}
""")

        eq_(flatten_result(l.get_template('main').render(x=2)),
            "this is main. this is secondary. x is 2")
コード例 #29
0
    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!"
                ]
コード例 #30
0
    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'
            ]