HTML Templating Using Selmer

Web Development with Clojure, Third Edition — by Dmitri Sotnikov, Scot Brown (18 / 107)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Define the Routes with Rei tit | TOC | What You’ve Learned 👉

Selmer is a general-purpose templating engine that’s content-agnostic. While it’s primarily geared toward working with HTML, it can be used to transform any kind of text. If you’re already familiar with Django or similar templating languages, you should feel right at home using Selmer.

When applied to generating HTML templates, Selmer encourages a clean separation between the presentation and the business logic. On top of that, the templates can be maintained by someone who has no knowledge of Clojure. Let’s create a new project where we’ll learn to use different features of Selmer. We’ll use the default Leiningen template for this purpose.

​ ​$ ​​lein​​ ​​new​​ ​​html-templating​

Once the project is created, add the Selmer dependency in the project.clj file.

​ [selmer ​"1.12.31"​]

We’re now ready to take a look at what Selmer has to offer using the REPL. Open up the html-templating.core namespace and add a reference to Selmer there.

​ (​ns​ html-templating.core
​ (:require [selmer.parser :as selmer]))

Once we’ve added the reference, we should be able to load the namespace and test that everything is working by evaluating this command in the REPL:

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.