예제 #1
0
    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)
예제 #2
0
    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"
예제 #3
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."
         ]
예제 #4
0
    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"]
예제 #5
0
    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"]
예제 #6
0
 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)
예제 #7
0
    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",
        ]
예제 #8
0
    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",
        ]
예제 #9
0
    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",
        ]
예제 #10
0
    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",
        ]
예제 #11
0
    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]",
        ]
        eq_(m.kwargs, {"dir": module_base, "type": "dbm"})
예제 #12
0
    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"],
        )
예제 #13
0
    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",
        ]
예제 #14
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"
            ]
예제 #15
0
    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",
        ]
예제 #16
0
    def test_def(self):
        t = Template("""
        <%!
            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 == {}
예제 #17
0
    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"})
예제 #18
0
    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",
        ]
예제 #19
0
    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"
            ]
예제 #20
0
    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",
        ]
예제 #21
0
    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"})
예제 #22
0
    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",
        ]
예제 #23
0
    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}
        """,
        )

        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",
            ]
예제 #24
0
    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",
        ]
예제 #25
0
    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 == {}
예제 #26
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"]
예제 #27
0
    def test_fileargs_deftag(self):
        t = Template("""
        <%%!
            callcount = [0]
        %%>
        <%%def name="foo()" cached="True" cache_type='file' cache_dir='%s'>
            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 == {"type": "file", "dir": module_base}
예제 #28
0
    def test_canget_kwargs(self):
        """test that arguments passed to the body()
        function are accessible by top-level defs"""
        l = lookup.TemplateLookup()
        l.put_string(
            "base",
            """

        ${next.body(x=12)}

        """,
        )

        l.put_string(
            "main",
            """
            <%inherit file="base"/>
            <%page args="x"/>
            this is main.  x is ${x}

            ${a()}

            <%def name="a(**args)">
                this is a, x is ${x}
            </%def>
        """,
        )

        # test via inheritance
        eq_(
            result_lines(l.get_template("main").render()),
            ["this is main. x is 12", "this is a, x is 12"],
        )

        l.put_string(
            "another",
            """
            <%namespace name="ns" file="main"/>

            ${ns.body(x=15)}
        """,
        )
        # test via namespace
        eq_(
            result_lines(l.get_template("another").render()),
            ["this is main. x is 15", "this is a, x is 15"],
        )
예제 #29
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",
        ]
예제 #30
0
    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")