mirror of
https://github.com/pantor/inja.git
synced 2026-04-04 15:18:53 +00:00
Remove unneed linking in update_loop_data (#85)
Commit a5862a0 made the linking step in update_loop_data unecessary
because the loop's copy of the data was made to come from the parent
loop's data rather than the original client's data.
While at it, also add one more nested loop test case.
This commit is contained in:
@@ -106,21 +106,9 @@ class Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
void update_loop_data(bool link = true) {
|
||||
void update_loop_data() {
|
||||
LoopLevel& level = m_loop_stack.back();
|
||||
|
||||
if (link && (m_loop_stack.size() > 1)) {
|
||||
for (int i = m_loop_stack.size() - 2; i >= 0; i--) {
|
||||
auto& level_it = m_loop_stack.at(i);
|
||||
|
||||
if (level_it.loop_type == LoopLevel::Type::Array) {
|
||||
level.data[static_cast<std::string>(level_it.value_name)] = level_it.values.at(level_it.index);
|
||||
} else {
|
||||
level.data[static_cast<std::string>(level_it.value_name)] = *level_it.map_it->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (level.loop_type == LoopLevel::Type::Array) {
|
||||
level.data[static_cast<std::string>(level.value_name)] = level.values.at(level.index); // *level.it;
|
||||
auto& loopData = level.data["loop"];
|
||||
@@ -551,7 +539,7 @@ class Renderer {
|
||||
break;
|
||||
}
|
||||
|
||||
update_loop_data(false);
|
||||
update_loop_data();
|
||||
|
||||
// jump back to start of loop
|
||||
i = bc.args - 1; // -1 due to ++i in loop
|
||||
|
||||
@@ -2710,21 +2710,9 @@ class Renderer {
|
||||
}
|
||||
}
|
||||
|
||||
void update_loop_data(bool link = true) {
|
||||
void update_loop_data() {
|
||||
LoopLevel& level = m_loop_stack.back();
|
||||
|
||||
if (link && (m_loop_stack.size() > 1)) {
|
||||
for (int i = m_loop_stack.size() - 2; i >= 0; i--) {
|
||||
auto& level_it = m_loop_stack.at(i);
|
||||
|
||||
if (level_it.loop_type == LoopLevel::Type::Array) {
|
||||
level.data[static_cast<std::string>(level_it.value_name)] = level_it.values.at(level_it.index);
|
||||
} else {
|
||||
level.data[static_cast<std::string>(level_it.value_name)] = *level_it.map_it->second;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (level.loop_type == LoopLevel::Type::Array) {
|
||||
level.data[static_cast<std::string>(level.value_name)] = level.values.at(level.index); // *level.it;
|
||||
auto& loopData = level.data["loop"];
|
||||
@@ -3155,7 +3143,7 @@ class Renderer {
|
||||
break;
|
||||
}
|
||||
|
||||
update_loop_data(false);
|
||||
update_loop_data();
|
||||
|
||||
// jump back to start of loop
|
||||
i = bc.args - 1; // -1 due to ++i in loop
|
||||
|
||||
@@ -70,6 +70,34 @@ TEST_CASE("types") {
|
||||
// CHECK_THROWS_WITH( env.render("{% for name in relatives %}{{ name }}{% endfor %}", data), "[inja.exception.json_error] [json.exception.type_error.302] type must be array, but is object" );
|
||||
}
|
||||
|
||||
SECTION("nested loops") {
|
||||
auto ldata = json::parse(
|
||||
R"DELIM(
|
||||
{ "outer" : [
|
||||
{ "inner" : [
|
||||
{ "in2" : [ 1, 2 ] },
|
||||
{ "in2" : []},
|
||||
{ "in2" : []}
|
||||
]
|
||||
},
|
||||
{ "inner" : [] },
|
||||
{ "inner" : [
|
||||
{ "in2" : [ 3, 4 ] },
|
||||
{ "in2" : [ 5, 6 ] }
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
)DELIM"
|
||||
);
|
||||
CHECK(env.render(R"DELIM(
|
||||
{% for o in outer %}{% for i in o.inner %}{{loop.parent.index}}:{{loop.index}}::{{loop.parent.is_last}}
|
||||
{% for ii in i.in2%}{{ii}},{%endfor%}
|
||||
{%endfor%}{%endfor%}
|
||||
)DELIM",
|
||||
ldata) == "\n0:0::false\n1,2,\n0:1::false\n\n0:2::false\n\n2:0::true\n3,4,\n2:1::true\n5,6,\n\n");
|
||||
}
|
||||
|
||||
SECTION("conditionals") {
|
||||
CHECK( env.render("{% if is_happy %}Yeah!{% endif %}", data) == "Yeah!" );
|
||||
CHECK( env.render("{% if is_sad %}Yeah!{% endif %}", data) == "" );
|
||||
|
||||
Reference in New Issue
Block a user