Djinn: A Code Generator and Templating Language Inspired by Jinja2
Published
Code generators can be useful tools. I sometimes use the command line version of Jinja2 to generate highly redundant config files and other text files, but it’s feature-limited for transforming data. Obviously the author of Jinja2 thinks differently, but I wanted something like list comprehensions or D’s composable range algorithms.
I decided to make a tool that’s like Jinja2, but lets me generate complex files by transforming data with range
algorithms. The idea was dead simple: a templating language that gets rewritten directly to D code. That way it
supports everything D does, simply because it is D. I wanted a standalone code generator, but thanks to
D’s mixin
feature, the same templating language works as an embedded templating language (for HTML in a web app, for
example). (For more on that trick, see this post about translating
Brainfuck to D to machine code all at compile time using mixin
s.)
As usual, it’s on GitLab. The examples in this post can be found there, too.