Skip to content

Commit da5bbb1

Browse files
mhhollomonpantor
authored andcommitted
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.
1 parent a5862a0 commit da5bbb1

File tree

3 files changed

+32
-28
lines changed

3 files changed

+32
-28
lines changed

include/inja/renderer.hpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,9 @@ class Renderer {
106106
}
107107
}
108108

109-
void update_loop_data(bool link = true) {
109+
void update_loop_data() {
110110
LoopLevel& level = m_loop_stack.back();
111111

112-
if (link && (m_loop_stack.size() > 1)) {
113-
for (int i = m_loop_stack.size() - 2; i >= 0; i--) {
114-
auto& level_it = m_loop_stack.at(i);
115-
116-
if (level_it.loop_type == LoopLevel::Type::Array) {
117-
level.data[static_cast<std::string>(level_it.value_name)] = level_it.values.at(level_it.index);
118-
} else {
119-
level.data[static_cast<std::string>(level_it.value_name)] = *level_it.map_it->second;
120-
}
121-
}
122-
}
123-
124112
if (level.loop_type == LoopLevel::Type::Array) {
125113
level.data[static_cast<std::string>(level.value_name)] = level.values.at(level.index); // *level.it;
126114
auto& loopData = level.data["loop"];
@@ -551,7 +539,7 @@ class Renderer {
551539
break;
552540
}
553541

554-
update_loop_data(false);
542+
update_loop_data();
555543

556544
// jump back to start of loop
557545
i = bc.args - 1; // -1 due to ++i in loop

single_include/inja/inja.hpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2710,21 +2710,9 @@ class Renderer {
27102710
}
27112711
}
27122712

2713-
void update_loop_data(bool link = true) {
2713+
void update_loop_data() {
27142714
LoopLevel& level = m_loop_stack.back();
27152715

2716-
if (link && (m_loop_stack.size() > 1)) {
2717-
for (int i = m_loop_stack.size() - 2; i >= 0; i--) {
2718-
auto& level_it = m_loop_stack.at(i);
2719-
2720-
if (level_it.loop_type == LoopLevel::Type::Array) {
2721-
level.data[static_cast<std::string>(level_it.value_name)] = level_it.values.at(level_it.index);
2722-
} else {
2723-
level.data[static_cast<std::string>(level_it.value_name)] = *level_it.map_it->second;
2724-
}
2725-
}
2726-
}
2727-
27282716
if (level.loop_type == LoopLevel::Type::Array) {
27292717
level.data[static_cast<std::string>(level.value_name)] = level.values.at(level.index); // *level.it;
27302718
auto& loopData = level.data["loop"];
@@ -3155,7 +3143,7 @@ class Renderer {
31553143
break;
31563144
}
31573145

3158-
update_loop_data(false);
3146+
update_loop_data();
31593147

31603148
// jump back to start of loop
31613149
i = bc.args - 1; // -1 due to ++i in loop

test/unit-renderer.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,34 @@ TEST_CASE("types") {
7070
// 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" );
7171
}
7272

73+
SECTION("nested loops") {
74+
auto ldata = json::parse(
75+
R"DELIM(
76+
{ "outer" : [
77+
{ "inner" : [
78+
{ "in2" : [ 1, 2 ] },
79+
{ "in2" : []},
80+
{ "in2" : []}
81+
]
82+
},
83+
{ "inner" : [] },
84+
{ "inner" : [
85+
{ "in2" : [ 3, 4 ] },
86+
{ "in2" : [ 5, 6 ] }
87+
]
88+
}
89+
]
90+
}
91+
)DELIM"
92+
);
93+
CHECK(env.render(R"DELIM(
94+
{% for o in outer %}{% for i in o.inner %}{{loop.parent.index}}:{{loop.index}}::{{loop.parent.is_last}}
95+
{% for ii in i.in2%}{{ii}},{%endfor%}
96+
{%endfor%}{%endfor%}
97+
)DELIM",
98+
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");
99+
}
100+
73101
SECTION("conditionals") {
74102
CHECK( env.render("{% if is_happy %}Yeah!{% endif %}", data) == "Yeah!" );
75103
CHECK( env.render("{% if is_sad %}Yeah!{% endif %}", data) == "" );

0 commit comments

Comments
 (0)