mustache.clj is a (yet another) implementation of the Mustache template system for Clojure.
By preprocessing template into a tree like data structure, It's quite fast.
I initally write it for my part time project: Rssminer
- Rssminer need to fast with limited MEM and CPU
- It makes the i18n quite easy
- Mustache is used both server side and client side
- Clean compact code: the jar size is about 11k
- Zero dependency
- Fast: renders the
test/sample.tpl
with the test data about 500k times per seconds on 13 inch Macbook Air - For Clojure
[me.shenfeng/mustache "1.1"]
<!-- test/sample.tpl -->
<h1>{{ title }}</h1>
<p class="desc">{{ desc }}</p>
<ul>
{{#tags}}
<li class="tag">{{ tag }}</li>{{/tags}}
{{# hidden }}
this will not show, if hidden is false or empty list
{{/ hidden }}
</ul>
(deftemplate tmpl-fn (slurp "test/sample.tpl"))
(def data {:title "mustache.clj"
:desc "Logic-less {{mustache}} templates for Clojure"
:tags [{:tag "Clojure"}
{:tag "Mustache"}
{:tag "Performance"}]})
(println (tmpl-fn data))
<h1>mustache.clj</h1>
<p class="desc">Logic-less {{mustache}} templates for Clojure</p>
<ul>
<li class="tag">Clojure</li>
<li class="tag">Mustache</li>
<li class="tag">Performance</li>
</ul>
templates folder:
templates
├── admin.tpl => admin
├── login.tpl => login
├── m
│ ├── landing.tpl => m-landing
│ ├── p_header.tpl => m-p-header
│ └── subs.tpl => m-subs
└── tmpls
├── app
│ ├── feed_content.tpl => tmpls-app-feed-content
(gen-tmpls-from-folder "templates" [".tpl"]) ; generates the clojure fn
; now you can write something like this
(admin {:key "str" :array [1 2 3 5]})
gen-tmpls-from-resources
just like gen-tmpls-from-folder, except find templates files from classpath
You can pass a function (optional) to deftemplate
, mktmpls-from-folder
, mktmpls-from-resouces
, allows you to transform the template data
(defn add-gloal-data [data]
(assoc data
:dev? (config/dev?) ; distingish dev and prod
:server-host (get-in *current-req* [:header "host"]) ; stg1, test, prod host are different
;; other data, like different data based on local => for i18n
))
(gen-tmpls-from-folder "templates" [".tpl"] add-gloal-data)
- Set Delimiter is not implemented now. Anyway, why change {{}} to <% %>
- Lambdas is not implemented
use ?
to test value true. example
{{?links}}
links is value true, this string get outputed
{{/links}}
{{^links}}
links is value false, this string get outputed
{{/links}}
{{#links}}
this is repeated (count links) times
{{/links}}
When using ?, Mustache.clj will print a warnning to stderr
Distributed under the Eclipse Public License, the same as Clojure.