Template libraries

2015-06-01 tags template
  1. Hiccup
  2. Enlive
  3. Selmer
  4. Conclusion

Which template library should you use for web application?

Hiccup

https://github.com/weavejester/hiccup

Hiccup is a library for representing HTML in Clojure. It uses vectors to represent elements, and maps to represent the element's attributes.

In simple words, you are writing code in Clojure which is transformed into HTML.

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

result

<span class="foo">bar</span>

Writing whole HTML pages in Clojure doesn't make sense. But it is good to generate HTML in Clojure which you want to inject into HTML template and when you learn Clojure, because Hiccup is very simple library. There is also https://github.com/teropa/hiccups which lets you use Hiccup with ClojureScript. It’s worth to notice that the code is represented by clear data, so you can easily write functions to transform this data in the way that you want.

Enlive

https://github.com/cgrand/enlive

Enlive is a selector-based (à la CSS) templating library for Clojure.

In simple words you are using this like jQuery selectors. It parses clear HTML template, finds used selectors and changes the content. Templates are pure HTML. Dynamic part of site is defined in Clojure. Notice: HTML and dynamic content code in Clojure are separated.

Please keep attention on title and :head :title (à la CSS selector) in example below.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>This is a title placeholder</title>
  </head>
  <body>
  </body>
</html>
(html/deftemplate main-template "templates/html_file_above.html"
  []
  [:head :title] (html/content "Enlive starter kit"))

result

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Enlive starter kit</title>
  </head>
  <body>
  </body>
</html>

If you have some reasons to keep HTML templates separate from dynamic content it is a good choice. Also notice that frontend coders can use temporary content like in our example "This is a title placeholder". It makes their works easier during developing if they don't have coder skills, but makes coder work harder.

Selmer

https://github.com/yogthos/Selmer

A fast, Django inspired template system for Clojure.

Similar to Django (Python) and Blade (PHP).

It lets you mix HTML with dynamic content code.

<html>
  {% include "content.html" %}
</html>
<body>{{content}}</body>
(render-file "index.html" {:content "Example content of site"})

result

<html>
  <body>Example content of site</body>
</html>

As you can see it is "keep it simple" even with nested template files.

You can also do more dynamic things like in example below. It is code to show tags connected with article on this site.

{% if post.tags|not-empty %}
    tags 
    {% for tag in post.tags %}
        <a href="{{tag.uri}}">{{tag.name}}</a>
    {% endfor %}
{% endif %}

Conclusion

Personally, I recommend using Hiccup only for things like CMS modules where you don't want to have additional HTML files and have HTML output which can be modified by other modules. Enlive makes coders’ lives harder to make frontend designers’ lives easier - it doesn't make sense. If you want to code HTML pages, learn also to write simple code. Selmer is intuitive and "keeps it simple" - this is the biggest value.

In my opinion, use Hiccup for things with CMS behaviour and Selmer for everything else.