update: forloop variables

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

View file

@ -21,8 +21,8 @@ pub const RenderError = error{
Overflow, Overflow,
Unexpected, Unexpected,
UnsupportedExpression, UnsupportedExpression,
// } || FilterError || parser.ParserError || icons.SvgError || std.fs.File.OpenError; // } || FilterError || parser.ParserError || icons.SvgError || std.fs.File.OpenError;
} || FilterError || parser.ParserError || std.fs.File.OpenError; } || FilterError || parser.ParserError || std.fs.File.OpenError;
pub const Renderer = struct { pub const Renderer = struct {
context: *Context, context: *Context,
@ -230,11 +230,31 @@ pub const Renderer = struct {
else => return, else => return,
}; };
for (list) |item| { for (list, 0..) |item, i| {
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
try ctx.set(node.tag.?.body.@"for".loop_var, item); 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| { for (node.tag.?.body.@"for".body) |child| {
try self.renderNode(alloc, nodes, child, writer, &ctx, null); try self.renderNode(alloc, nodes, child, writer, &ctx, null);
@ -381,7 +401,7 @@ pub const Renderer = struct {
const svg_kind = node.tag.?.body.svg.kind; const svg_kind = node.tag.?.body.svg.kind;
const svg_name = node.tag.?.body.svg.name; const svg_name = node.tag.?.body.svg.name;
if (self.cache.icons.?.getIcon(alloc,svg_kind, svg_name)) |svg_content| { if (self.cache.icons.?.getIcon(alloc, svg_kind, svg_name)) |svg_content| {
try writer.writeAll("<div class=\"svg-container\">"); try writer.writeAll("<div class=\"svg-container\">");
try writer.writeAll(svg_content); try writer.writeAll(svg_content);
try writer.writeAll("</div>"); try writer.writeAll("</div>");