@@ -2,11 +2,6 @@ const Templates = @import("../template.zig");
const Template = Templates.Template;
const Directive = Templates.Directive;
pub const Injector = struct {
ctx: *anyopaque,
func: *const fn (*anyopaque, []const u8) ?[]const u8,
};
pub fn PageRuntime(comptime PageDataType: type) type {
return struct {
pub const Self = @This();
@@ -21,11 +16,7 @@ pub fn PageRuntime(comptime PageDataType: type) type {
};
}
pub fn format(self: Self, comptime f: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
return try self.format2(f, null, out);
}
pub fn format2(self: Self, comptime _: []const u8, injt: ?Injector, out: anytype) !void {
pub fn format(self: Self, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
//var ctx = self.data;
var blob = self.template.blob;
while (blob.len > 0) {
@@ -34,20 +25,11 @@ pub fn PageRuntime(comptime PageDataType: type) type {
blob = blob[offset..];
if (Directive.init(blob)) |drct| {
const end = drct.tag_block.len;
drct.formatTyped(PageDataType, self.data, injt, out) catch |err| switch (err) {
drct.formatTyped(PageDataType, self.data, out) catch |err| switch (err) {
error.IgnoreDirective => try out.writeAll(blob[0..end]),
error.VariableMissing => {
if (injt) |inj| {
if (inj.func(inj.ctx, blob[0..end])) |str| {
try out.writeAll(str);
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}} Injection failed", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
},
else => return err,
};
@@ -80,11 +62,7 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
return .{ .data = d };
}
pub fn format(self: Self, comptime f: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
return try self.format2(f, null, out);
}
pub fn format2(self: Self, comptime _: []const u8, injt: ?Injector, out: anytype) !void {
pub fn format(self: Self, comptime _: []const u8, _: std.fmt.FormatOptions, out: anytype) !void {
var blob = Self.PageTemplate.blob;
while (blob.len > 0) {
if (indexOfScalar(u8, blob, '<')) |offset| {
@@ -93,20 +71,11 @@ pub fn Page(comptime template: Template, comptime PageDataType: type) type {
if (Directive.init(blob)) |drct| {
const end = drct.tag_block.len;
drct.formatTyped(PageDataType, self.data, injt, out) catch |err| switch (err) {
drct.formatTyped(PageDataType, self.data, out) catch |err| switch (err) {
error.IgnoreDirective => try out.writeAll(blob[0..end]),
error.VariableMissing => {
if (injt) |inj| {
if (inj.func(inj.ctx, blob[0..end])) |str| {
try out.writeAll(str);
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}} Injection failed", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
} else {
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
}
if (!is_test) log.err("Template Error, variable missing {{{s}}}", .{blob[0..end]});
try out.writeAll(blob[0..end]);
},
else => return err,
};