I’m playing with an idea for a templating system for my web server. I basically want to have some templates, transpile them into Zig code at build time, and be able to import the app from the templates and the templates from the app. I’ve started with a new automatically-created module for the templates, but I’m going back to just a bunch of generated Zig files as it’s easier to cross-import things that way. Is there any project doing something like this? I’d like to see how others integrated it into the build system.
Done something similar where I generate the Zig files, and also an entry point that imports all the generated files, I then create a module with the entry point to use those files. In order to use/import other codes in the template I inject the required modules or if files just copy them to the respective directory and it just works.
Most of the things are here.
But curious to see if there are other better ways to do this.
Oh, I actually forgot to check how you do it in your project, which is what inspired me to play with this in the first place. You’ve have done AMAZING work on ziex. I’d so much want to use it directly, but the component model makes it hard, it requires state, allocations, etc. What I’m experimenting with is a much simpler parser (not tree-sitter) and directly outputting HTML in the built function.
I think you probably can look into this one GitHub - nektro/zig-pek: A comptime HTML preprocessor with a builtin template engine for Zig., and or Zine. Yeah for Ziex the allocations are necessary (temporarily since there are stack allocated content inside the function, and also sometime it is necessary to escape HTML).
i think vulkan-zig is a good example that comes to mind! basically make your generator a little executable that writes code to a file (or to stdout, either’s fine) and add a std.Build.Step.Run to run the generator exe.
for smaller things, i might just make a little std.Io.Writer.Allocating or whatever and print Zig code to it from within a function or a block in build.zig and put those contents into a std.Build.Step.WriteFiles
Zitron is a Zig code generator, you can see here how to integrate it into a build.zig. The documentation goes into slightly more detail, or at least has more comments.
Thank you! This was the key part I was missing. I needed to build the compiler exe in my build.zig and use the artifact in the app’s build.zig. I also added a small wrapper function to my build.zig that adds the relevant build steps.