Skip to content

Instantly share code, notes, and snippets.

@sherpc
Created November 27, 2017 13:48
Show Gist options
  • Save sherpc/77cf66b6dc1565d8c24c04f6b66e502b to your computer and use it in GitHub Desktop.
Save sherpc/77cf66b6dc1565d8c24c04f6b66e502b to your computer and use it in GitHub Desktop.
Mustache-like templating in Clojure
(ns mustache
(:require [clojure.string :as str]))
(defn mustache
[s args]
(reduce
(fn [result [k v]]
(str/replace result
(re-pattern (format "([^\\{])\\{%s\\}([^\\}])" (name k)))
(format "$1%s$2" v)))
s args))
(assert
(=
(mustache "a={a}, b={{a}}, c={c}" {:a 1 :b 2})
"a=1, b={{a}}, c={c}"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment