retain scope when including a template (#118)

* apply documentation changes to single_include

* retain scope when including a template

* Use data from current scope when rendering an included template.
  This allows included templates to access loop variables, which
  was not possible before.
* Add test
This commit is contained in:
Tom
2019-09-14 11:35:06 +02:00
committed by pantor
parent b46d92a793
commit 057075b110
3 changed files with 39 additions and 2 deletions

View File

@@ -380,6 +380,16 @@ TEST_CASE("templates") {
CHECK( env.render(t2, data) == "Hello Peter!" );
CHECK_THROWS_WITH( env.parse("{% include \"does-not-exist\" %}!"), "[inja.exception.file_error] failed accessing file at 'does-not-exist'" );
}
SECTION("include-in-loop") {
json loop_data;
loop_data["cities"] = json::array({{{"name", "Munich"}}, {{"name", "New York"}}});
inja::Environment env;
env.include_template("city.tpl", env.parse("{{ loop.index }}:{{ city.name }};"));
CHECK( env.render("{% for city in cities %}{% include \"city.tpl\" %}{% endfor %}", loop_data) == "0:Munich;1:New York;" );
}
}