Comment by stefs

16 hours ago

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.

Very neat. Almost reminds me of Android's databinding era (i.e. type-safe generated interfaces to XML Views). Though Android development quickly moved on to Compose UI, since Kotlin is expressive enough to cover all of the bases, I guess. And ever since then, I've personally grown annoyed with how much other ecosystems rely on templating. (In fact, I'm doubly annoyed that JetBrains picked YAML for Amper/Kotlin Toolchain. Since they had to add special templating to it anyway, why wouldn't they just use Kotlin or some subset?)

Nice work. This is a good approach IMHO, and (if you need validation) also the one adopted by Templ in Go, which has a 'generate' command to do this.