Execute else branches

This commit is contained in:
jaina heartles 2022-12-07 17:30:01 -08:00
parent bb297a4f6e
commit f8a799abfb
2 changed files with 10 additions and 3 deletions

View file

@ -52,7 +52,11 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
.@"if" => |if_stmt| { .@"if" => |if_stmt| {
const condition = evaluateExpression(if_stmt.header.condition, args, captures); const condition = evaluateExpression(if_stmt.header.condition, args, captures);
const subtemplate = if_stmt.subtemplate; const subtemplate = if_stmt.subtemplate;
if (condition) try executeTemplate(writer, subtemplate, args, captures); if (condition) {
try executeTemplate(writer, subtemplate, args, captures);
} else {
if (if_stmt.else_branch) |branch| try executeTemplate(writer, branch, args, captures);
}
}, },
//else => @compileError("TODO"), //else => @compileError("TODO"),
} }
@ -178,6 +182,8 @@ fn parseTemplate(
.@"if" = .{ .@"if" = .{
.subtemplate = subtemplate.items, .subtemplate = subtemplate.items,
.header = header, .header = header,
.else_branch = if (else_subtemplate) |sub| sub.items else null,
}, },
}, },
}}; }};
@ -516,6 +522,7 @@ const ForHeader = struct {
const If = struct { const If = struct {
subtemplate: []const TemplateItem, subtemplate: []const TemplateItem,
header: IfHeader, header: IfHeader,
else_branch: ?[]const TemplateItem,
}; };
const IfHeader = struct { const IfHeader = struct {

View file

@ -14,8 +14,8 @@
{$b}: {$b}:
{= /for =} {= /for =}
{= /for} {= /for}
{#if .qux}qux!{#else}!qux!{/if=} {#if .qux}qux{#else}!qux{/if}
{#if .quxx}quxx!{#else}!quxx!{/if} {#if .quxx}quxx{#else}!quxx{/if}
</section> </section>
</body> </body>
</html> </html>