Zig's comptime makes HTML templates type-safe and allocation-free
Type-safe HTML templating in Zig
Lukas Lalinsky built zt, a Zig templating library inspired by Go's Templ. Templates are transpiled to Zig during zig build, so inputs are fully typed and errors surface at compile time. All dynamic values are HTML-escaped by default, and output goes straight to std.Io.Writer with no allocations. It supports reusable components, @children layouts, and plain Zig control flow, making it practical for htmx or Datastar SPAs.
What I like the most is that template errors are just Zig compiler errors.
- solatic
I love this design, especially as it takes advantage of the Zig build chain; I expect re-builds will be fast enough that you essentially get JS-style hot reloading in the dev cycle. Now we just need https://github.com/air-verse/air for Zig (Ziex has one built into its CLI, but that's only for Ziex)...
Looking forward to seeing some benchmarks!
- pjmlp
> Two years ago, I picked up Zig, and I’d never have imagined I’d be considering web development in such a low-level language.
This is kind of funny remark, given that the author started in 2000 in Web development, and with PHP 3.0.
I started in 1997, our distributed systems class had exercises to write CGIs in Perl and C.
Between 1999 and 2001, most Web development at my employer was in Tcl and C, ASP was naturally a mix of VB and C++ COM components, and there was also the fun to write all those mod_something Apache extensions, and IIS ISAPI modules, naturally in C and C++.
Ah, Microsoft was also shipping ATLServer, because who wouldn't want to use ATL for Web development, while Borland/Inprise was shipping similar stuff for Delphi and C++ Builder.
PHP itself, in 3.0, it was still the time that it hardly had much functionality builtin, so it was quite common to write extension modules in C.
So it is kind of ironic to read coming back to the way things were, before the slow scripting languages takevover in the 2010's, is something no one thought about.
- stefs
i'm also using a "type safe" templating engine (for kotlin, as a gradle plugin) of my own design that goes the other way - instead of embedding a model class into the template, a template class is generated that takes all the values as parameters.
i.e. MyTemplate.tpl.html
{if $foo}hello world, {$myname}!{else}goodbye, world.{/if}
which creates a kotlin class that can be used like this:
MyTemplate(
foo = true,
myname = "bar"
).toString()
the biggest upside for me is that this way there's full, free IDE support, i don't need to write extra view models if the mapping isn't 1:1 and performance is pretty good because in the end there's no runtime parsing/compiling/interpreting except for the baseline JDK stuff (though there are even faster engines).
https://codeberg.org/h34tnet/tempolin
warning: so far i'm the only user though, so don't use it in production. also, i'm working on some improvements right now which haven't been pushed yet.
- fergie
W3C: Why can't we just have <includes src="./header.html"> and be done with it?
- weitzj
Thank you for the article. I remember using Google Soy templates roughly 10 years ago. I think this is similar to what is used now when you make Helm templates for example or Go Templates.
It has the html escaping, JavaScript escaping etc built in.
Is there maybe a Zig wrapper for Google soy around?