mirror of
https://github.com/pantor/inja.git
synced 2026-07-03 07:44:26 +00:00
allow empty lists for loops
This commit is contained in:
@@ -380,6 +380,7 @@ public:
|
||||
template<typename T = json>
|
||||
T eval_expression(const Parsed::ElementExpression& element, const json& data) {
|
||||
const json var = eval_function(element, data);
|
||||
if (var.empty()) return T();
|
||||
try {
|
||||
return var.get<T>();
|
||||
} catch (json::type_error& e) {
|
||||
|
||||
@@ -56,6 +56,9 @@ TEST_CASE("types") {
|
||||
CHECK( env.render("{% for v in vars %}{% if v > 0 %}+{% endif %}{% endfor %}", data) == "+++" );
|
||||
// CHECK( env.render("{% if 1 >= 18 %}test{% endif %}{% for v in vars %}{% if v > 0 %}+{% else %}-{% endif %}{% endfor %}", data) == "+++----" );
|
||||
|
||||
data["empty_loop"] = {};
|
||||
CHECK( env.render("{% for name in empty_loop %}a{% endfor %}", data) == "" );
|
||||
|
||||
CHECK_THROWS_WITH( env.render("{% for name ins names %}a{% endfor %}", data), "[inja.exception.parser_error] unknown loop statement: for name ins names" );
|
||||
// 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" );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user