How the templating works

How the templating works

There are two pieces:

  1. src/template.zig — a Mustache subset. Templates parse once

into a flat opcode list; rendering walks the opcodes and writes bytes directly into a std.Io.Writer.

  1. src/markdown.zig — a minimal Markdown to HTML renderer.

Single-pass; no allocations.

At startup, src/site.zig walks site/templates/ and site/posts/, runs each post's body through the markdown renderer, and wraps the result in the post and layout templates. The fully-rendered HTML ends up in an arena that lives for the process's lifetime.

The HTTP handler then looks like this:

fn onPost(req: *SmallReq, res: *http.Response) void {
    const slug = req.pathParam("slug") orelse return;
    if (site.postHtml(slug)) |html| {
        res.setHtml(html);
    } else {
        res.setStatus(404);
        res.setHtml(site.notFoundHtml());
    }
}

That's the whole story.