Skip to content

flocktory/wet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Aleksey Kuleshov
Feb 1, 2019
f008193 Â· Feb 1, 2019

History

58 Commits
Aug 14, 2018
Aug 14, 2018
Feb 1, 2019
Jan 27, 2017
Jan 24, 2017
Feb 1, 2019
Feb 1, 2019

Repository files navigation

wet 💧

Build Status

wet is a pure Clojure/ClojureScript port of the Liquid template language built on top of Instaparse.

Installation

Leiningen/Boot

[com.flocktory/wet "0.2.1"]

CLI

{:deps {com.flocktory/wet {:mvn/version "0.2.1"}}}

Usage

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])

An example

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.

Thanks

Aleksey Burlak