HubSpot / jinjava

Jinja template engine for Java
Apache License 2.0
690 stars 168 forks source link

Make null values loop-able. #1140

Closed hs-lsong closed 8 months ago

hs-lsong commented 9 months ago

When the loop collection contains null, the loop var will be searching the outer scope of the value instead of just use the null value. For example,

context.put("number", -1);
context.put("the_list", Lists.newArrayList(1L, 2L, null, null, null));
String template = "{% for number in the_list %} {{ number }} {% endfor %}";

When rendering the above for tag, because the 3rd and later values are null, the loop var is number, and it will find the value of it from outside of the forTag scope. It will result in the following

Expected :" 1  2  null  null  null "
Actual   :" 1  2  -1  -1  -1 "

With this special NullValue instance, the loopVar value will stay inside the loop.