Esempio n. 1
0
 def test_recursive_loop_compile(self, env):
     Template("""
         {% for p in foo recursive%}
             {{p.bar}}
             {% for f in p.fields recursive%}
                 {{f.baz}}
                 {{p.bar}}
                 {% if f.rec %}
                     {{ loop(f.sub) }}
                 {% endif %}
             {% endfor %}
         {% endfor %}
         """)
     Template("""
         {% for p in foo%}
             {{p.bar}}
             {% for f in p.fields recursive%}
                 {{f.baz}}
                 {{p.bar}}
                 {% if f.rec %}
                     {{ loop(f.sub) }}
                 {% endif %}
             {% endfor %}
         {% endfor %}
         """)
Esempio n. 2
0
    def test_call_with_args(self, env):
        t = Template("""{% macro dump_users(users) -%}
        <ul>
          {%- for user in users -%}
            <li><p>{{ user.username|e }}</p>{{ caller(user) }}</li>
          {%- endfor -%}
          </ul>
        {%- endmacro -%}

        {% call(user) dump_users(list_of_user) -%}
          <dl>
            <dl>Realname</dl>
            <dd>{{ user.realname|e }}</dd>
            <dl>Description</dl>
            <dd>{{ user.description }}</dd>
          </dl>
        {% endcall %}""")

        assert [
            x.strip() for x in t.render(list_of_user=[{
                "username": "******",
                "realname": "something else",
                "description": "test",
            }]).splitlines()
        ] == [
            u"<ul><li><p>apo</p><dl>",
            u"<dl>Realname</dl>",
            u"<dd>something else</dd>",
            u"<dl>Description</dl>",
            u"<dd>test</dd>",
            u"</dl>",
            u"</li></ul>",
        ]
Esempio n. 3
0
def test_iterator_not_advanced_early():
    t = Template("{% for _, g in gs %}{{ loop.index }} {{ g|list }}\n{% endfor %}")
    out = t.render(
        gs=itertools.groupby([(1, "a"), (1, "b"), (2, "c"), (3, "d")], lambda x: x[0])
    )
    # groupby groups depend on the current position of the iterator. If
    # it was advanced early, the lists would appear empty.
    assert out == "1 [(1, 'a'), (1, 'b')]\n2 [(2, 'c')]\n3 [(3, 'd')]\n"
Esempio n. 4
0
def test_generator_stop():
    class X(object):
        def __getattr__(self, name):
            raise StopIteration()

    t = Template("a{{ bad.bar() }}b")
    with pytest.raises(RuntimeError):
        t.render(bad=X())
Esempio n. 5
0
 def test_else_loop_bug(self, env):
     t = Template("""
         {% for x in y %}
             {{ loop.index0 }}
         {% else %}
             {% for i in range(3) %}{{ i }}{% endfor %}
         {% endfor %}
     """)
     assert t.render(y=[]).strip() == "012"
Esempio n. 6
0
 def test_stacked_locals_scoping_bug_twoframe(self, env):
     t = Template("""
         {% set x = 1 %}
         {% for item in foo %}
             {% if item == 1 %}
                 {% set x = 2 %}
             {% endif %}
         {% endfor %}
         {{ x }}
     """)
     rv = t.render(foo=[1]).strip()
     assert rv == u"1"
Esempio n. 7
0
    def test_contextfunction_callable_classes(self, env):
        from jinja.utils import contextfunction

        class CallableClass(object):
            @contextfunction
            def __call__(self, ctx):
                return ctx.resolve("hello")

        tpl = Template("""{{ callableclass() }}""")
        output = tpl.render(callableclass=CallableClass(), hello="TEST")
        expected = "TEST"

        assert output == expected
Esempio n. 8
0
def parse_vars(yamlin):
    # save the initial object
    if 'global_yaml' not in globals():
        global global_yaml
        global_yaml = yamlin

    # recursively go through the object
    if isinstance(yamlin, list):
        yamlout = []
        for sub in yamlin:
            yamlout.append(parse_vars(sub))
    elif isinstance(yamlin, dict):
        yamlout = {}
        for sub in yamlin:
            yamlout[sub] = parse_vars(yamlin[sub])
    # treat each string as jinja template
    elif isinstance(yamlin, str):
        yamlout = Template(yamlin).render(global_yaml)
    # be sure nothing gets lost
    else:
        yamlout = yamlin
    return yamlout
Esempio n. 9
0
def test_loop_idx0():
    t = Template(TEST_IDX0_TEMPLATE_STR_1)
    lst = [10]
    excepted_render = "[(len=1, revindex0=0, index0=0, val=10)]"
    assert excepted_render == t.render(lst=lst)
Esempio n. 10
0
 def assert_error(code, expected):
     with pytest.raises(TemplateSyntaxError, match=expected):
         Template(code)