minijinja.zig
thekorn/minijinja.zig
Apache-2.0
zig bindings for minijinja
1 1 0 0
3
bindings, zig, zig-package
build.zig.zon
build.zig
View on Github
thekorn/minijinja.zig
zig bindings for minijinja
zig bindings for the amazing minijinja templating engine. It's a thin wrapper around the C ABI of minijinja.
NOTE: this is a work in progress and mac only at the moment.
nix develop
to get a shell with all dependencies)Add this package to your zig project:
$ zig fetch --save git+https://github.com/thekorn/minijinja.zig#main
Add dependency and import to the build.zig
file:
...
const minijinja = b.dependency("minijinja", .{
.target = target,
.optimize = optimize,
});
...
exe.root_module.addImport("minijinja", minijinja.module("minijinja"));
And then, just use it in the code:
const std = @import("std");
const minijinja = @import("minijinja");
const Environment = minijinja.Environment;
const Context = minijinja.Context;
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
const templ =
\\<ul>
\\{%- for user in users %}
\\ <li>{{ user.name }}</li>
\\{%- endfor %}
\\</ul>
;
var env = try Environment.init();
defer env.deinit();
env.set_debug(true);
try env.add_template("list", templ);
const User = struct {
name: []const u8,
};
const users = [_]User{
.{ .name = "Hans" },
.{ .name = "Wurst" },
};
const t = try env.render_template_struct(allocator, "list", .{ .users = users });
defer allocator.free(t);
std.debug.print("{s}\n", .{t});
}
Which results in
<ul>
<li>Hans</li>
<li>Wurst</li>
</ul>
Using nix
tests can be run like
$ nix develop -c zig build test --summary all