Something went wrong. Try again.
Templating language written in Zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293const std = @import("std");const Writer = std.Io.Writer;
const build_options = @import("build_options");
pub const zmd = @import("zmd");pub const jetcommon = @import("jetcommon");
// XXX: Ensure that `@import("zmpl").zmpl` always works. This is a workaround to allow Zmpl to be// imported at build time because `@import("zmpl")` at build time imports `zmpl/build.zig`.pub const zmpl = @This();
/// Generic, JSON-compatible data type.pub const Data = @import("zmpl/Data.zig");pub const Template = @import("zmpl/Template.zig");pub const Manifest = Template.Manifest;pub const templates = Template.Manifest;pub const colors = @import("zmpl/colors.zig");pub const Format = @import("zmpl/Format.zig");pub const debug = @import("zmpl/debug.zig");
pub const isZmplValue = Data.isZmplValue;
pub const InitOptions = struct { templates_path: []const u8 = "src/templates",};
pub const util = @import("zmpl/util.zig");
pub const find = Manifest.find;pub const findPrefixed = Manifest.findPrefixed;
pub fn chomp(input: []const u8) []const u8 { return std.mem.trimRight(u8, input, "\r\n");}
/// Sanitize input. Used internally for rendering data refs. Use `zmpl.fmt.sanitize` to manually/// sanitize other values.pub fn sanitize(writer: anytype, input: []const u8) !void { if (!build_options.sanitize) { _ = try writer.write(input); return; }
const fmt = Format{ .writer = if (@TypeOf(writer) == *Data) &writer.output_buf.writer else writer }; _ = try fmt.sanitize(input);}
/// Check if a value is present for use in if conditions./// This is used to make nullable values behave intuitively in if statements./// For example, `@if (foo.bar)` will be true if `foo.bar` is not null.pub fn isPresent(value: anytype) !bool { const T = @TypeOf(value);
// Handle null values if (T == @TypeOf(null)) return false;
// Handle optional values if (@typeInfo(T) == .optional) { if (value == null) return false; return try isPresent(value.?); }
// Handle ZmplValue if (comptime isZmplValue(T)) { return value.isPresent(); }
// For booleans, return the value directly if (T == bool) return value;
// For strings, check if the string is not empty if (comptime std.meta.trait.isZigString(T)) { return value.len > 0; }
// For numbers, check if the value is not zero if (comptime std.meta.trait.isNumber(T)) { return value != 0; }
// Default to true for any other value that exists return true;}
pub fn refIsPresent(data: *Data, ref_key: []const u8) !bool { return data.refPresence(ref_key);}
test { std.testing.refAllDecls(@This());}