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
When the loop collection contains
null
, the loop var will be searching the outer scope of the value instead of just use thenull
value. For example,When rendering the above for tag, because the 3rd and later values are
null
, the loop var isnumber
, and it will find the value of it from outside of the forTag scope. It will result in the followingWith this special
NullValue
instance, the loopVar value will stay inside the loop.