Genearte .zig verions of templates to use in production.
This commit is contained in:
33
benchmarks/compiled/helpers.zig
Normal file
33
benchmarks/compiled/helpers.zig
Normal file
@@ -0,0 +1,33 @@
|
||||
// Auto-generated helpers for compiled Pug templates
|
||||
// This file is copied to the generated directory to provide shared utilities
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
/// Append HTML-escaped string to buffer
|
||||
pub fn appendEscaped(buf: *std.ArrayListUnmanaged(u8), allocator: std.mem.Allocator, str: []const u8) !void {
|
||||
for (str) |c| {
|
||||
switch (c) {
|
||||
'&' => try buf.appendSlice(allocator, "&"),
|
||||
'<' => try buf.appendSlice(allocator, "<"),
|
||||
'>' => try buf.appendSlice(allocator, ">"),
|
||||
'"' => try buf.appendSlice(allocator, """),
|
||||
'\'' => try buf.appendSlice(allocator, "'"),
|
||||
else => try buf.append(allocator, c),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a value is truthy (for conditionals)
|
||||
pub fn isTruthy(val: anytype) bool {
|
||||
const T = @TypeOf(val);
|
||||
return switch (@typeInfo(T)) {
|
||||
.bool => val,
|
||||
.int, .float => val != 0,
|
||||
.pointer => |ptr| switch (ptr.size) {
|
||||
.slice => val.len > 0,
|
||||
else => true,
|
||||
},
|
||||
.optional => if (val) |v| isTruthy(v) else false,
|
||||
else => true,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user