diff --git a/src/renderer.zig b/src/renderer.zig index 5064dcb..c899961 100644 --- a/src/renderer.zig +++ b/src/renderer.zig @@ -21,8 +21,8 @@ pub const RenderError = error{ Overflow, Unexpected, UnsupportedExpression, -// } || FilterError || parser.ParserError || icons.SvgError || std.fs.File.OpenError; -} || FilterError || parser.ParserError || std.fs.File.OpenError; + // } || FilterError || parser.ParserError || icons.SvgError || std.fs.File.OpenError; +} || FilterError || parser.ParserError || std.fs.File.OpenError; pub const Renderer = struct { context: *Context, @@ -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); @@ -381,7 +401,7 @@ pub const Renderer = struct { const svg_kind = node.tag.?.body.svg.kind; 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("
"); try writer.writeAll(svg_content); try writer.writeAll("
");