1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00
zap/examples/mustache/mustache.zig
sadbeast 8f5aa17109 Add MustacheLoad to support loading from file
According to the facil.io docs
(http://facil.io/0.7.x/fiobj_mustache#fiobj_mustache_new):

> By setting the filename argument even when the data argument exists,
> it will allow path resolution for partial templates. Otherwise, there
> is no way to know where to find the partial templates.

This will allow partial templates to work.

However, this also introduces a breaking change to the existing
MustacheNew function. This change makes it mirror the C version where it
accepts a MustacheLoadArgs struct instead of just the data.  That's also
a handy method to have, so I renamed that to MustacheData...maybe
there's a better name?
2023-09-13 13:22:23 +02:00

62 lines
1.6 KiB
Zig

const std = @import("std");
const zap = @import("zap");
fn on_request(r: zap.SimpleRequest) void {
const template = "{{=<< >>=}}* Users:\r\n<<#users>><<id>>. <<& name>> (<<name>>)\r\n<</users>>\r\nNested: <<& nested.item >>.";
const p = zap.MustacheData(template) catch return;
defer zap.MustacheFree(p);
const User = struct {
name: []const u8,
id: isize,
};
const ret = zap.MustacheBuild(p, .{
.users = [_]User{
.{
.name = "Rene",
.id = 1,
},
.{
.name = "Caro",
.id = 6,
},
},
.nested = .{
.item = "nesting works",
},
});
defer ret.deinit();
if (r.setContentType(.TEXT)) {
if (ret.str()) |s| {
r.sendBody(s) catch return;
} else {
r.sendBody("<html><body><h1>MustacheBuild() failed!</h1></body></html>") catch return;
}
} else |err| {
std.debug.print("Error while setting content type: {}\n", .{err});
}
}
pub fn main() !void {
var listener = zap.SimpleHttpListener.init(.{
.port = 3000,
.on_request = on_request,
.log = true,
.max_clients = 100000,
});
try listener.listen();
// zap.enableDebugLog();
// zap.debug("ZAP debug logging is on\n", .{});
// we can also use facilio logging
// zap.Log.fio_set_log_level(zap.Log.fio_log_level_debug);
// zap.Log.fio_log_debug("hello from fio\n");
std.debug.print("Listening on 0.0.0.0:3000\n", .{});
// start worker threads
zap.start(.{
.threads = 1,
.workers = 1,
});
}