wet is a pure Clojure/ClojureScript port of the Liquid template language built on top of Instaparse.
[amperity/wet "0.2.5"]
{:deps {amperity/wet {:mvn/version "0.2.5"}}}
In the vein of the original library,
wet provides a minimalistic interface comprising parse
and render
functions.
Calling wet from Clojure and ClojureScript is completely identical.
wet.core
is the only namespace you are going to need.
(:require [wet.core :as wet])
Prepare the template:
(def template
(str "{{ season | capitalize }} kept us warm, {{ 'cover' | gerund }} "
"{{ planets.habitable[0] }} in forgetful snow, {{ 'feed' | gerund }} "
"a {{ size }} life with dried tubers."))
;; An intermediate representation of the parsed template
(def parsed-template
(wet/parse template))
It may also be convenient to request a rudimentary template analysis
from the parser prior to rendering with the :analyse?
option.
That way, a basic summary of the template's contents can be collected
from the parsed template's metadata.
(def parsed-template
(wet/parse template {:analyse? true}))
(meta parsed-template)
=> {:lookups #{"season" "planet" "size"},
:core-filters #{"capitalize"},
:custom-filters #{"gerund"}}
Finally, obtain the rendered result:
(wet/render
parsed-template
;; :params may contain any Clojure data structures
{:params {:season "winter"
:planets {"habitable" ["Earth" "Mars"]}
:size "little"}
;; Any Clojure function of arity one or more may act as a Liquid filter
;; when passed in the :filters map. The first argument is the object
;; being transformed, and the rest is passed to the filter as parameters.
;; For detailed examples on filters please consult wet.filters-test.
:filters {:gerund (fn [verb] (str verb "ing"))}})
=> "Winter kept us warm, covering Earth in forgetful snow, feeding a little life with dried tubers."
The complete list of core Liquid filters can be found in
wet.filters
.