update: forloop variables

This commit is contained in:
Lucas F. 2026-01-24 22:40:35 -03:00
parent 20e4bacab0
commit 47f52fea58

View file

@ -230,11 +230,31 @@ pub const Renderer = struct {
else => return,
};
for (list) |item| {
for (list, 0..) |item, i| {
var ctx = Context.init(alloc);
defer ctx.deinit();
try ctx.set(node.tag.?.body.@"for".loop_var, item);
try ctx.set("forloop.counter", i + 1);
try ctx.set("forloop.counter0", i);
try ctx.set("forloop.revcounter", (list.len - i));
try ctx.set("forloop.revcounter0", (list.len - i) - 1);
try ctx.set("forloop.first", i == 0);
try ctx.set("forloop.last", i == (list.len - 1));
try ctx.set("forloop.length", list.len);
// forloop.counter
// The current iteration of the loop (1-indexed)
// forloop.counter0
// The current iteration of the loop (0-indexed)
// forloop.revcounter
// The number of iterations from the end of the loop (1-indexed)
// forloop.revcounter0
// The number of iterations from the end of the loop (0-indexed)
// forloop.first
// True if this is the first time through the loop
// forloop.last
// True if this is the last time through the loop
// forloop.length
for (node.tag.?.body.@"for".body) |child| {
try self.renderNode(alloc, nodes, child, writer, &ctx, null);