From f8a799abfb63ec577ce9f59695a817db1b1e6d49 Mon Sep 17 00:00:00 2001 From: jaina heartles Date: Wed, 7 Dec 2022 17:30:01 -0800 Subject: [PATCH] Execute else branches --- src/template/lib.zig | 9 ++++++++- src/template/test.tmp.html | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/template/lib.zig b/src/template/lib.zig index ec9f130..e64289c 100644 --- a/src/template/lib.zig +++ b/src/template/lib.zig @@ -52,7 +52,11 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca .@"if" => |if_stmt| { const condition = evaluateExpression(if_stmt.header.condition, args, captures); 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"), } @@ -178,6 +182,8 @@ fn parseTemplate( .@"if" = .{ .subtemplate = subtemplate.items, .header = header, + + .else_branch = if (else_subtemplate) |sub| sub.items else null, }, }, }}; @@ -516,6 +522,7 @@ const ForHeader = struct { const If = struct { subtemplate: []const TemplateItem, header: IfHeader, + else_branch: ?[]const TemplateItem, }; const IfHeader = struct { diff --git a/src/template/test.tmp.html b/src/template/test.tmp.html index 56e41a5..6d62925 100644 --- a/src/template/test.tmp.html +++ b/src/template/test.tmp.html @@ -14,8 +14,8 @@ {$b}: {= /for =} {= /for} - {#if .qux}qux!{#else}!qux!{/if=} - {#if .quxx}quxx!{#else}!quxx!{/if} + {#if .qux}qux{#else}!qux{/if} + {#if .quxx}quxx{#else}!quxx{/if}