Created
November 27, 2017 13:48
-
-
Save sherpc/77cf66b6dc1565d8c24c04f6b66e502b to your computer and use it in GitHub Desktop.
Mustache-like templating in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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