In praise of Hiccup

I’m writing a simple CRUD web application for a school course and I’m doing it in Clojure. I chose to do the templating with Hiccup. It’s a Clojure library for generating HTML. You specify the structure as inline data, like this:

(require '[hiccup.page :as h])

(defn example-page [title]
  (h/html5
   [:head
    [:title title]]
   [:body
    [:div.container
     [:h1 title]
     [:p "Hello world!"]]]))

For a long time, there has been a debate how much or little logic you should have in your templates. On the one end of spectrum is Mustache, which advertises itself as logic-less. On the other end is PHP, where all you have are templates and programs are fully embedded in them.

I’m unsure which one is the right way, but I do say this: if you choose to embed logic in your templates, you should use a proper programming language. This is what Hiccup does: Hiccup templates are just Clojure code that generates plain old Clojure data. Contrast this to e.g. Django’s templating language, which allows you to do a lot of things, but never quite as much as you’d want.


Comments or questions? Send me an e-mail.