Type-safe HTML templating in Zig
I've been involved in web development for many years, starting around the turn of the century when PHP 3.0 first gained traction. Throughout my career, I've experimented with various methods for generating HTML, including raw PHP, string concatenation in Python, Smarty templates in PHP, Template Toolkit in Perl, and Jinja/Django templates in Python.
However, as the web evolved to emphasize SPAs and JavaScript frameworks, my frontend work diminished. Two years ago, I began exploring Zig, a low-level language that I never anticipated would lead me back to web development.
During my exploration, I discovered zio and Dusty, an ecosystem I initially built for API backends using JSON or MessagePack. Despite the challenges of managing memory with arena allocators, I found Zig's comptime feature enabled me to create high-level interfaces without sacrificing performance. This led me to consider web development, even SPAs, without resorting to JavaScript or TypeScript. I was particularly drawn to the idea of building templating libraries that could meet my requirements.
My search for a suitable templating solution proved fruitless until I discovered Ziex, which introduced me to the JSX approach that could be applied outside of JavaScript. The developer experience Nurul Huda achieved in Zig was truly remarkable, and despite not initially aligning with my needs due to a preference for non-WASM frameworks, Ziex's syntax and method of integrating Zig code into templates inspired me. Further research revealed Templ for Go, which solidified my vision for building a similar library in Zig.
Thus, I present zt, an adaptation of Templ tailored for Zig. Templates are parsed and transpiled to Zig code during the build process, allowing them to be imported directly from application code. More importantly, they can also import application code, enabling parameters to be fully typed. The generated code is straightforward, focusing on writing to a std.Io.Writer, which can be directed to a socket or accumulated in a buffer as needed.
Templates can be utilized from Zig code as demonstrated in the examples provided. For integration with Dusty to render HTML pages, the usage is seamless. It's worth noting that a bug related to error handling in dynamic values may arise when using cancellation in std.Io, which I plan to address in an upcoming blog post. The render function in Dusty, which wraps the std.Io.Writer handling, resolves this issue.
By default, all dynamic values are HTML-escaped in both text and attributes, and values in URL attributes such as href or src are checked for dangerous schemes. To output raw HTML explicitly, the pattern {!value} must be used, making it easily identifiable in the template. The parser also enforces proper element closure, preventing the accidental creation of broken markup and reducing common XSS vulnerabilities.
What I appreciate most about zt is how template errors manifest as Zig compiler errors. Any typos, such as {u.nmae} instead of {u.name}, result in clear and informative errors pointing to the generated code, complete with comments indicating the original template's location. This approach extends to calling code, ensuring that passing a string where a ?User is expected will not compile.
Beyond these basics, you can create reusable components and pass them as values, and with @children, build layouts that wrap other templates—similar to template inheritance in other languages.
Control flow in zt mirrors standard Zig constructs, allowing for if, for, and switch statements to function as expected, including payload captures. The resulting output is compiled down to a series of writes without any allocations, maintaining high performance. An example application showcasing zt alongside other Dusty components used for building dynamic SPAs is available in the TODO example within Dusty.
While the syntax is still evolving, particularly regarding block-level control flow, which is expressed as plain Zig following Templ's example, I welcome feedback. The project is still in its early stages, and I encourage any comments or feedback on GitHub or through the Zig Discord.
Rewritten by urgent.news; may contain errors — check the original.
Source: lalinsky.com
This story
This is one outlet's version. Read the fullest account.
- Type-safe HTML templating in Zig lalinsky.com