How to do templates properly

There are numerous templating engines out there. There is:

  • Jinja2 - which is standard these days
  • Python’s Django’s templates - which are much like jinja2, but not quite
  • Go’s text/template which claims to be the template engine (hence, the name)
  • Mustache, which is also popular
  • And numerous others

I’ve even recently stumbled upon Calibre’s (which is an open-source e-book management tool) own custom (!!!) templating engine. The fact that I have to learn another templating engine so that my e-books can be placed into folders is insane.

The issue here is that every one of them is a DSL, a separate language which you have to memorize. Sometimes, it becomes even more problematic, such as in the case of jinja2 vs Django, where you have to remember the differences between one and the other.

I claim that I don’t need any of it (you might not need as well).

In my actual job, I write low-level software in C. Fortunately, there aren’t many templates there.

However, from time to time, I need to write a small piece of software, like a couple of page python website or some script to create config files.

The thing I’ve discovered is that there is no real need to learn additional language. You already have your main one! If you have obtained some python knowledge (which probably was kinda easy), you already have the skill to use string interpolation, if-else and for loops. And you don’t need anything else to cover 90% usages of the template engine.

The reason why I find it much easier is that python has become largely ingrained into my brain, and I am capable of opening up my text editor and starting writing code, even if I haven’t touched it for a while.

This is not the case for template engines.

Despite being a somewhat experienced developer, I am struggling with remembering: was it {{ var }} or {{​% var }} or {% var %} or whatever.

So, I decided to stop using templates in personal scripts and projects, and just use python.

One example where I practiced it was a config file for Sway and i3. These are two mostly compatible window managers on Linux, for Wayland and X11 respectively. Sway is a project aimed at being a reimplementation of i3, but for Wayland, and is mostly config-file compatible. However, that’s not the case for small details (e.g. both display pipeline setup and input management are separate in X11, but embedded to the composers in Wayland). With this, I found the need to use two similar configs.

I wrote a script which reuses most parts of config files, but tweaks it slightly. It turns out with Python I don’t need to remember how things are done in whatever template engine, and can also do more complex things.

Another two examples come from the web development field:

  • A quick prototype a short narrative game in Go
  • A web representation of a people database.

In both of these cases, I initially started with templates. But as I went, I realized, that again, I didn’t remember the syntax details of a template engine, and just opted to interpolate and concat strings. The issue here is that you lose your editor’s completion and highlighting, but that’s a technical issue, solvable by improving tooling.


And if we think about less experienced developers, this becomes a case even more.

During my whole professional career, I’ve also been involved in teaching programming to high- and middle-school students. Tere was an interesting case I observed while a friend of mine was teaching web development to a beginner group. Students there have not yet grasped the basic concepts of python, but needed to learn jinja2 templating, to be able to do web development like it is believed to be done properly. In fact, the friend of mine has even struggled to explain to them that python and jinja2 are two different languages, each with its own syntax. This is a perfect case to eliminate additional language and let students obtain more practice with basic python constructions.

This logic probably has its limitations. One may be skeptical of this approach, thinking of PHP, which has its idea of intertwined logic and representation as a part of a language. In general, this concern generalizes to the fact, that this approach would break an MVC model. However, nothing prevents you from separating of “python templates” into separate files, thinking of it as the V in MVC. Also, you might actually not need MVC for your project, but concepts around software architecture I would prefer to address in another post.

To conclude, I’ve stopped trying to fit a template engine into my head, and you should do the same.

P.S.: This blog post is written with Hugo. Its template engine feels threatened by the content, and resisting:

Error: error building site: assemble: 
failed to extract shortcode: template for shortcode "var" not found

This is from mention of {{​%. I actually had to put a ZERO WIDTH SPACE between {{ and % to make it compile.