Skip to content

Commit b776a46

Browse files
committed
fix #235
mutable reference to vector of variants triggered a situation when destructor cleared a global var after template rendering add a test for regression
1 parent 3fc48f3 commit b776a46

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/template_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class TemplateImpl : public ITemplateImpl
251251
InternalValueMap extParams;
252252
InternalValueMap intParams;
253253

254-
auto convertFn = [&intParams](auto& params) {
254+
auto convertFn = [&intParams](const auto& params) {
255255
for (auto& ip : params)
256256
{
257257
auto valRef = &ip.second.data();

test/basic_tests.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,28 @@ from Parser!)";
645645
EXPECT_STREQ(expectedResult.c_str(), result.c_str());
646646
}
647647

648+
TEST(BasicTests, EnvTestPreservesGlobalVar)
649+
{
650+
jinja2::TemplateEnv tplEnv;
651+
tplEnv.AddGlobal("global_var", jinja2::Value("foo"));
652+
tplEnv.AddGlobal("global_fn", jinja2::MakeCallable([]() {
653+
return "bar";
654+
}));
655+
std::string result1;
656+
{
657+
jinja2::Template tpl(&tplEnv);
658+
tpl.Load("Hello {{ global_var }} {{ global_fn() }}!!!");
659+
result1 = tpl.RenderAsString(jinja2::ValuesMap{}).value();
660+
}
661+
std::string result2;
662+
{
663+
jinja2::Template tpl(&tplEnv);
664+
tpl.Load("Hello {{ global_var }} {{ global_fn() }}!!!");
665+
result2 = tpl.RenderAsString(jinja2::ValuesMap{}).value();
666+
}
667+
ASSERT_EQ(result1, result2);
668+
}
669+
648670
MULTISTR_TEST(BasicMultiStrTest, LiteralWithEscapeCharacters, R"({{ 'Hello\t\nWorld\n\twith\nescape\tcharacters!' }})", "Hello\t\nWorld\n\twith\nescape\tcharacters!")
649671
{
650-
}
672+
}

0 commit comments

Comments
 (0)