update: now

This commit is contained in:
Lucas F. 2026-01-18 07:26:34 -03:00
parent 36cc1caac0
commit 1888c5e07a
2 changed files with 37 additions and 0 deletions

View file

@ -10,6 +10,7 @@ const builtin_filters = @import("filters.zig").builtin_filters;
const FilterError = @import("filters.zig").FilterError;
const parser = @import("parser.zig");
const TemplateCache = @import("cache.zig").TemplateCache;
const time = @import("time.zig");
pub const RenderError = error{
InvalidCharacter,
@ -308,6 +309,12 @@ pub const Renderer = struct {
try writer.writeAll(std.fmt.allocPrint(alloc, "{d}", .{ratio}) catch "0");
},
.now =>{
var format: []const u8 = node.tag.?.body.now.format;
if (format.len == 0) format = "Y-m-d H:i:s";
const datetime = try time.Time.now().toStringAlloc(alloc, format);
try writer.writeAll(datetime);
},
else => {},
}
},

View file

@ -635,3 +635,33 @@ test "renderer - widthratio inside block" {
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo padrão") != null);
try testing.expect(std.mem.indexOf(u8, output, "Conteúdo do filho") != null);
}
test "renderer - now" {
std.debug.print("____________________________________________________\n", .{});
std.debug.print("15 - render now\n", .{});
const alloc = testing.allocator;
var ctx = Context.init(alloc);
defer ctx.deinit();
var cache = TemplateCache.init(alloc);
defer cache.deinit();
const renderer = Renderer.init(&ctx, &cache);
try ctx.set("idade", Value{ .int = 20 });
const template =
// \\{% now \"Y-m-d H:i:s\" %}
\\{% now %}
;
var buf = std.ArrayList(u8){};
defer buf.deinit(alloc);
try renderer.renderString(template, buf.writer(alloc));
std.debug.print("OUTPUT:\n\n{s}\n", .{buf.items});
// try testing.expect(std.mem.indexOf(u8, buf.items, "Maior") != null);
}