def test_scoped_block_after_inheritance(self, env): env = Environment(loader=DictLoader({ "layout.html": """ {% block useless %}{% endblock %} """, "index.html": """ {%- extends 'layout.html' %} {% from 'helpers.html' import foo with context %} {% block useless %} {% for x in [1, 2, 3] %} {% block testing scoped %} {{ foo(x) }} {% endblock %} {% endfor %} {% endblock %} """, "helpers.html": """ {% macro foo(x) %}{{ the_foo + x }}{% endmacro %} """, })) rv = env.get_template("index.html").render(the_foo=42).split() assert rv == ["43", "44", "45"]
def test_scopes_and_blocks(self): env = Environment( loader=DictLoader({ "a.html": """ {%- set foo = 'bar' -%} {% include 'x.html' -%} """, "b.html": """ {%- set foo = 'bar' -%} {% block test %}{% include 'x.html' %}{% endblock -%} """, "c.html": """ {%- set foo = 'bar' -%} {% block test %}{% set foo = foo %}{% include 'x.html' %}{% endblock -%} """, "x.html": """{{ foo }}|{{ test }}""", })) a = env.get_template("a.html") b = env.get_template("b.html") c = env.get_template("c.html") assert a.render(test="x").strip() == "bar|x" assert b.render(test="x").strip() == "bar|x" assert c.render(test="x").strip() == "bar|x"
def test_import_from_with_context(self): env = Environment(loader=DictLoader( {"a": "{% macro x() %}{{ foobar }}{% endmacro %}"})) t = env.from_string( "{% set foobar = 42 %}{% from 'a' import x with context %}{{ x() }}" ) assert t.render() == "42"
def test_set_and_include(self): env = Environment(loader=DictLoader( { "inc": "bar", "main": '{% set foo = "foo" %}{{ foo }}{% include "inc" %}', })) assert env.get_template("main").render() == "foobar"
def test_loop_include(self): env = Environment(loader=DictLoader({ "inc": "{{ i }}", "main": '{% for i in [1, 2, 3] %}{% include "inc" %}{% endfor %}', })) assert env.get_template("main").render() == "123"
def test_context_include_with_overrides(self, test_env): env = Environment(loader=DictLoader( dict( main= "{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}", item="{{ item }}", ))) assert env.get_template("main").render() == "123"
def test_include(self, env_trim): env_trim = Environment( loader=DictLoader( {"include": "{% macro test(foo) %}[{{ foo }}]{% endmacro %}"} ) ) tmpl = env_trim.from_string('{% from "include" import test %}{{ test("foo") }}') assert tmpl.render() == "[foo]"
def test_scopes_and_include(self): env = Environment(loader=DictLoader( { "include.html": "{{ var }}", "base.html": '{% include "include.html" %}', "child.html": '{% extends "base.html" %}{% set var = 42 %}', })) t = env.get_template("child.html") assert t.render() == "42"
def test_env(): env = Environment(loader=DictLoader( dict( module="{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}", header="[{{ foo }}|{{ 23 }}]", o_printer="({{ o }})", ))) env.globals["bar"] = 23 return env
def test_super_in_scoped_block(self, env): env = Environment(loader=DictLoader({ "master.html": "{% for item in seq %}[{% block item scoped %}" "{{ item }}{% endblock %}]{% endfor %}" })) t = env.from_string('{% extends "master.html" %}{% block item %}' "{{ super() }}|{{ item * 2 }}{% endblock %}") assert t.render(seq=list(range(5))) == "[0|0][1|2][2|4][3|6][4|8]"
def test_extends_output_bugs(self, env): env = Environment(loader=DictLoader( {"parent.html": "(({% block title %}{% endblock %}))"})) t = env.from_string( '{% if expr %}{% extends "parent.html" %}{% endif %}' "[[{% block title %}title{% endblock %}]]" "{% for item in [1, 2, 3] %}({{ item }}){% endfor %}") assert t.render(expr=False) == "[[title]](1)(2)(3)" assert t.render(expr=True) == "((title))"
def test_scoped_block(self, env): env = Environment(loader=DictLoader({ "master.html": "{% for item in seq %}[{% block item scoped %}" "{% endblock %}]{% endfor %}" })) t = env.from_string( "{% extends 'master.html' %}{% block item %}{{ item }}{% endblock %}" ) assert t.render(seq=list(range(5))) == "[0][1][2][3][4]"
def test_preserve_blocks(self, env): env = Environment(loader=DictLoader({ "a": "{% if false %}{% block x %}A{% endblock %}" "{% endif %}{{ self.x() }}", "b": '{% extends "a" %}{% block x %}B{{ super() }}{% endblock %}', })) tmpl = env.get_template("b") assert tmpl.render() == "BA"
def test_dynamic_inheritance(self, env): env = Environment(loader=DictLoader( { "master1": "MASTER1{% block x %}{% endblock %}", "master2": "MASTER2{% block x %}{% endblock %}", "child": "{% extends master %}{% block x %}CHILD{% endblock %}", })) tmpl = env.get_template("child") for m in range(1, 3): assert tmpl.render(master="master%d" % m) == "MASTER%dCHILD" % m
def env(): return Environment( loader=DictLoader({ "layout": LAYOUTTEMPLATE, "level1": LEVEL1TEMPLATE, "level2": LEVEL2TEMPLATE, "level3": LEVEL3TEMPLATE, "level4": LEVEL4TEMPLATE, "working": WORKINGTEMPLATE, "doublee": DOUBLEEXTENDS, }), trim_blocks=True, )
def test_multi_inheritance(self, env): env = Environment(loader=DictLoader({ "master1": "MASTER1{% block x %}{% endblock %}", "master2": "MASTER2{% block x %}{% endblock %}", "child": """{% if master %}{% extends master %}{% else %}{% extends 'master1' %}{% endif %}{% block x %}CHILD{% endblock %}""", })) tmpl = env.get_template("child") assert tmpl.render(master="master2") == "MASTER2CHILD" assert tmpl.render(master="master1") == "MASTER1CHILD" assert tmpl.render() == "MASTER1CHILD"
def test_super(self, env): env = Environment(loader=DictLoader({ "a": "{% block intro %}INTRO{% endblock %}|" "BEFORE|{% block data %}INNER{% endblock %}|AFTER", "b": '{% extends "a" %}{% block data %}({{ ' "super() }}){% endblock %}", "c": '{% extends "b" %}{% block intro %}--{{ ' "super() }}--{% endblock %}\n{% block data " "%}[{{ super() }}]{% endblock %}", })) tmpl = env.get_template("c") assert tmpl.render() == "--INTRO--|BEFORE|[(INNER)]|AFTER"
def test_custom_context(self, env): from jinja.runtime import Context class MyContext(Context): pass class MyEnvironment(Environment): context_class = MyContext loader = DictLoader({ "base": "{{ foobar }}", "test": '{% extends "base" %}' }) env = MyEnvironment(loader=loader) assert env.get_template("test").render(foobar="test") == "test"
def test_fixed_macro_scoping_bug(self, env): assert (Environment(loader=DictLoader({ "test.html": """\ {% extends 'details.html' %} {% macro my_macro() %} my_macro {% endmacro %} {% block inner_box %} {{ my_macro() }} {% endblock %} """, "details.html": """\ {% extends 'standard.html' %} {% macro my_macro() %} my_macro {% endmacro %} {% block content %} {% block outer_box %} outer_box {% block inner_box %} inner_box {% endblock %} {% endblock %} {% endblock %} """, "standard.html": """ {% block content %} {% endblock %} """, })).get_template("test.html").render().split() == [ u"outer_box", u"my_macro" ])
@contextfunction def gettext(context, string): language = context.get("LANGUAGE", "en") return languages.get(language, {}).get(string, string) @contextfunction def ngettext(context, s, p, n): language = context.get("LANGUAGE", "en") if n != 1: return languages.get(language, {}).get(p, p) return languages.get(language, {}).get(s, s) i18n_env = Environment(loader=DictLoader(i18n_templates), extensions=["jinja.ext.i18n"]) i18n_env.globals.update({ "_": gettext, "gettext": gettext, "ngettext": ngettext }) i18n_env_trimmed = Environment(extensions=["jinja.ext.i18n"]) i18n_env_trimmed.policies["ext.i18n.trimmed"] = True i18n_env_trimmed.globals.update({ "_": gettext, "gettext": gettext, "ngettext": ngettext }) newstyle_i18n_env = Environment(loader=DictLoader(newstyle_i18n_templates),
def test_correct_prefix_loader_name(self, env): env = Environment(loader=PrefixLoader({"foo": DictLoader({})})) with pytest.raises(TemplateNotFound) as e: env.get_template("foo/bar.html") assert e.value.name == "foo/bar.html"
def test_block_set_with_extends(self): env = Environment(loader=DictLoader( {"main": "{% block body %}[{{ x }}]{% endblock %}"})) t = env.from_string('{% extends "main" %}{% set x %}42{% endset %}') assert t.render() == "[42]"