(#63) Allow invoking other templates

Closes #63
This commit is contained in:
jaina heartles 2022-12-07 23:34:57 -08:00
parent e072b045c6
commit 8c0548fc74
3 changed files with 29 additions and 21 deletions

View file

@ -227,7 +227,7 @@ pub const Response = struct {
defer stream.close();
const writer = stream.writer();
try @import("template").execute(writer, templ, data);
try @import("template").execute(writer, .{}, templ, data);
try stream.finish();
}

View file

@ -1,7 +1,11 @@
const std = @import("std");
pub fn main() !void {
try execute(std.io.getStdOut().writer(), @embedFile("./test.tmp.html"), .{
try execute(
std.io.getStdOut().writer(),
.{ .test_tmpl = "{.x}" },
@embedFile("./test.tmp.html"),
.{
.community = .{ .name = "abcd" },
.foo = [_][]const u8{ "5", "4", "3", "2", "1" },
.baz = [_][]const []const u8{
@ -11,27 +15,28 @@ pub fn main() !void {
.bar = .{ .x = "x" },
.qux = false,
.quxx = true,
});
},
);
}
pub fn execute(writer: anytype, comptime template: []const u8, args: anytype) !void {
pub fn execute(writer: anytype, comptime other_templates: anytype, comptime template: []const u8, args: anytype) !void {
@setEvalBranchQuota(@intCast(u32, template.len * 8));
const tokens = comptime parseTemplateTokens(ControlTokenIter{ .text = template });
const tmpl = comptime parseTemplate(tokens, 0, .root);
try executeTemplate(writer, tmpl.items, args, .{});
try executeTemplate(writer, other_templates, tmpl.items, args, .{});
}
fn executeTemplate(writer: anytype, comptime items: []const TemplateItem, args: anytype, captures: anytype) !void {
fn executeTemplate(writer: anytype, comptime templates: anytype, comptime items: []const TemplateItem, args: anytype, captures: anytype) !void {
inline for (items) |it| {
switch (it) {
.text => |text| try writer.writeAll(text),
.statement => |stmt| try executeStatement(writer, stmt, args, captures),
.statement => |stmt| try executeStatement(writer, templates, stmt, args, captures),
}
}
}
fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void {
fn executeStatement(writer: anytype, comptime templates: anytype, comptime stmt: Statement, args: anytype, captures: anytype) !void {
switch (stmt) {
.expression => |expr| {
const val = evaluateExpression(expr, args, captures);
@ -44,6 +49,7 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
for (iterable) |v| {
try executeTemplate(
writer,
templates,
subtemplate,
args,
addCapture(captures, loop.header.capture, v),
@ -54,16 +60,18 @@ fn executeStatement(writer: anytype, comptime stmt: Statement, args: anytype, ca
const condition = evaluateExpression(if_stmt.header.condition, args, captures);
const subtemplate = if_stmt.subtemplate;
if (condition) {
try executeTemplate(writer, subtemplate, args, captures);
try executeTemplate(writer, templates, subtemplate, args, captures);
} else {
if (if_stmt.else_branch) |branch| switch (branch) {
.@"else" => |subtmpl| try executeTemplate(writer, subtmpl, args, captures),
.elif => |elif| try executeStatement(writer, .{ .@"if" = elif.* }, args, captures),
.@"else" => |subtmpl| try executeTemplate(writer, templates, subtmpl, args, captures),
.elif => |elif| try executeStatement(writer, templates, .{ .@"if" = elif.* }, args, captures),
};
}
},
.call_template => |call| {
std.log.debug("calling template {s} with arg {any}", .{ call.template_name, call.args });
const new_template = @field(templates, call.template_name);
try execute(writer, templates, new_template, evaluateExpression(call.args, args, captures));
//std.log.debug("calling template {s} with arg {any}", .{ call.template_name, call.args });
},
//else => @compileError("TODO"),
}

View file

@ -22,7 +22,7 @@
neither
{=/if}
{#template test_tmpl .bar}
<template>{#template test_tmpl .bar}</template>
</section>
</body>
</html>