Add loop captures to capture struct
This commit is contained in:
parent
61493dc797
commit
c9d0090ab2
2 changed files with 26 additions and 3 deletions
|
@ -39,12 +39,12 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
|
|||
const iterable = deref(args, fields);
|
||||
const subtemplate = loop.subtemplate;
|
||||
try writer.writeAll("capture on " ++ loop.capture ++ ": ");
|
||||
for (iterable) |_| {
|
||||
for (iterable) |v| {
|
||||
try executeTemplate(
|
||||
writer,
|
||||
subtemplate,
|
||||
args,
|
||||
captures,
|
||||
addCapture(captures, loop.capture, v),
|
||||
);
|
||||
}
|
||||
},
|
||||
|
@ -74,6 +74,29 @@ fn deref(arg: anytype, comptime names: []const []const u8) Deref(@TypeOf(arg), n
|
|||
return deref(@field(arg, names[0]), names[1..]);
|
||||
}
|
||||
|
||||
fn AddCapture(comptime Root: type, comptime name: []const u8, comptime Val: type) type {
|
||||
var fields = std.meta.fields(Root) ++ [_]std.builtin.Type.StructField{.{
|
||||
.name = name,
|
||||
.field_type = Val,
|
||||
.default_value = null,
|
||||
.is_comptime = false,
|
||||
.alignment = @alignOf(Val),
|
||||
}};
|
||||
|
||||
return @Type(.{ .Struct = .{
|
||||
.layout = .Auto,
|
||||
.fields = fields,
|
||||
.decls = &.{},
|
||||
.is_tuple = false,
|
||||
} });
|
||||
}
|
||||
|
||||
fn addCapture(root: anytype, comptime name: []const u8, val: anytype) AddCapture(@TypeOf(root), name, @TypeOf(val)) {
|
||||
var result = std.mem.zeroInit(AddCapture(@TypeOf(root), name, @TypeOf(val)), root);
|
||||
@field(result, name) = val;
|
||||
return result;
|
||||
}
|
||||
|
||||
const TemplateType = enum {
|
||||
root,
|
||||
subtemplate,
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
<section>
|
||||
{$bar}
|
||||
{#for .foo |$f|}a testing thing {#end_for}
|
||||
{#for .foo |$f|}a testing thing: {$f} {#end_for}
|
||||
{{#for args.notes |$note, $i|}}
|
||||
<h3>Note no. {{$i}}</h3>
|
||||
{{#template note_display ($note)}}
|
||||
|
|
Loading…
Reference in a new issue