← Back to context

Comment by Tomte

5 days ago

Your wife’s Python version is quite impressive. It wouldn’t have occurred to me to do the simple thing and just do some string-replacement targeted at a narrow use-case instead of using a complicated templating engine.

> just do some string-replacement targeted at a narrow use-case instead of using a complicated templating engine.

A neat little in-between "string replacements" and "flown blown templating" is doing something like what hiccup introduced, basically using built-in data structures as the template. Hiccup looks something like this:

    (h/html [:span {:class "foo"} "bar"])

And you get both the power of templates, something easier than "templating engine" and with the extra benefit of being able to use your normal programming language functions to build the "templates".

I also implemented something similar myself (called niccup) that also does the whole "data to html" shebang but with Nix and only built-in Nix types. So for my own website/blog, I basically do things like this:

    nav = [ "nav"
      [ "h2" "Posts" ]
      [ "ul" (map (p: [ "li" [ "a" { href = "${p.slug}.html"; } p.title ] ]) posts) ]
    ]

And it's all "native" Nix, but "compiles" to HTML at build-time, great for static websites :)

> Your wife’s Python version is quite impressive.

Thank you. That was, in fact, the inspiration behind writing my own in CL.