Lightweight Clojure library for simple string templating?
There are quite a lot of templating libraries. Some general purpose ones are:
- clostache
- comb
- selmer
Note: If you just need to have some clever formatting, you have the standard library clojure.pprint with the cl-format
function and clojure.core/format
which is the wrapper around java.util.Formatter
.
You can use the <<
string interpolation macro from the core.incubator project.
To use it, add [org.clojure/core.incubator "0.1.4"]
as a dependency in your project.clj
file. (Note: check core.incubator's GitHub page for the latest installation instructions.)
Example use:
(ns example
(:require [clojure.core.strint :refer [<<]]))
(def my-name "XYZ")
(<< "My name is ~{my-name}.")
; Returns: "My name is XYZ."
(let [x 3
y 4]
(<< "~{x} plus ~{y} equals ~(+ x y)."))
; Returns: "3 plus 4 equals 7."
Note the difference between the use of curly braces ~{}
and parentheses ~()
.