minijinja.zig
minijinja.zig

thekorn/minijinja.zig

Apache-2.0

zig bindings for minijinja

1 1 0 0
3
build.zig.zon  build.zig 
View on Github  
Updated: 2:16:06 AM Tue Oct 22 2024 Size: 53KB Created: 6:43:22 AM Sat Oct 19 2024
Dependencies:
minijinja
zig  fetch  --save  git+https://github.com/thekorn/minijinja.zig

minijinja.zig

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.

Requirements

  • zig >= 0.14
  • rustup (for building minijinja-capi) or
  • a nix environment (and then use nix develop to get a shell with all dependencies)

Usage

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>

Tests

Using nix tests can be run like

$ nix develop -c zig build test --summary all