We are using a generator to input data to jinja2 templates. When we render, the data is streamed out using TemplateStream, which works great unless we have loop.last in our template.
The code for loop.last currently reads in the whole iterator (in our case a generator which reads files) in order to determine the length of the loop, and check to see if this is the last iteration.
There is an alternative to checking the length, which is to keep the next element in the iteration stored in memory, and checking that element for a sentinel value to see if the loop is on the last iteration.
That is the approach I took in this pull request. We are patching our jinja2 packages internally, in order to allow us to use loop.last on big data (though loop.length is still dangerous, and by extension, the repr method on LoopContext). Thanks.
Fixed loop.last to not consume the entire iterator to determine if th…
the last iteration of the loop.
Added changelog entry for #92 and changed LoopContext.End to _last_it…