mirror of
https://github.com/pantor/inja.git
synced 2026-04-16 13:08:51 +00:00
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:
@@ -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;" );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user