update: full template

This commit is contained in:
Lucas F. 2026-01-14 14:09:09 -03:00
parent 0116e84c09
commit eaff212b63

View file

@ -9,33 +9,40 @@ pub fn main() !void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const alloc = arena.allocator(); const alloc = arena.allocator();
const base = // const base =
\\<html> // \\<html>
\\<head><title>{% block title %}Título Padrão{% endblock %}</title></head> // \\<head><title>{% block title %}Título Padrão{% endblock %}</title></head>
\\<body> // \\<body>
\\{% block content %}Conteúdo padrão{% endblock %} // \\{% block content %}Conteúdo padrão{% endblock %}
\\</body> // \\</body>
\\</html> // \\</html>
; // ;
//
// const child =
// \\{% extends "base.html" %}
// \\{% block title %}Meu Título{% endblock %}
// \\{% block content %}
// \\Olá {{ nome }}!
// \\{% endblock %}
// ;
//
// try std.fs.cwd().writeFile(.{
// .sub_path = "base.html",
// .data = base,
// });
// try std.fs.cwd().writeFile(.{
// .sub_path = "child.html",
// .data = child,
// });
// defer std.fs.cwd().deleteFile("base.html") catch {};
// defer std.fs.cwd().deleteFile("child.html") catch {};
const child =
\\{% extends "base.html" %}
\\{% block title %}Meu Título{% endblock %}
\\{% block content %}
\\Olá {{ nome }}!
\\{% endblock %}
;
try std.fs.cwd().writeFile(.{ const User =struct {
.sub_path = "base.html", name: []const u8,
.data = base, email: []const u8,
}); notifications: i64 = 0
try std.fs.cwd().writeFile(.{ };
.sub_path = "child.html",
.data = child,
});
defer std.fs.cwd().deleteFile("base.html") catch {};
defer std.fs.cwd().deleteFile("child.html") catch {};
var ctx = Context.init(alloc); var ctx = Context.init(alloc);
defer ctx.deinit(); defer ctx.deinit();
@ -45,12 +52,27 @@ pub fn main() !void {
const renderer = Renderer.init(&ctx, &cache); const renderer = Renderer.init(&ctx, &cache);
const user = User{
.name = "Lucas",
.email = "lucas@email",
.notifications = 5
};
const itens = [3][]const u8{"Livro", "Caneta", "Caderno"};
try ctx.set("nome", "Lucas"); try ctx.set("nome", "Lucas");
try ctx.set("user", user);
try ctx.set("msg", "Bazinga!");
try ctx.set("itens", itens);
for(ctx.get("itens").?.list) |item| {
std.debug.print(" - {s}\n", .{item.string});
}
var buffer = std.ArrayList(u8){}; var buffer = std.ArrayList(u8){};
defer buffer.deinit(ctx.allocator()); defer buffer.deinit(ctx.allocator());
try renderer.render("child.html", buffer.writer(ctx.allocator())); try renderer.render("home.html", buffer.writer(ctx.allocator()));
const output = buffer.items; const output = buffer.items;