Mustache

is a full Mustache templating implementation for Carp.

A template is rendered via Mustache.template against a context of type (Map String Mustache). The Mustache sumtype carries the supported value kinds: strings, lists, nested maps, and lambdas.

(Mustache.template
  "Hello, {{#users}}{{name}} {{/users}}!"
  &{@"users" (Mustache.Lst [(Box.init (Mustache.Mp {@"name" (Box.init (Mustache.Str @"Ada"))}))
                              (Box.init (Mustache.Mp {@"name" (Box.init (Mustache.Str @"Grace"))}))])})
; => "Hello, Ada Grace !"

Supports comments, section iteration, inverted sections, object contexts, partials, and the set-delimiter tag. Templates are parsed into an intermediate node tree before rendering, so nested sections work correctly.

Templates are processed at byte level, which is correct for ASCII and UTF-8 input as long as the delimiters themselves are ASCII.

Lambda

instantiate

(Fn [(Fn [String] String)] Mustache)

creates a Lambda.

Lst

instantiate

(Fn [(Array (Box Mustache))] Mustache)

creates a Lst.

Mp

instantiate

(Fn [(Map String (Box Mustache))] Mustache)

creates a Mp.

Str

instantiate

(Fn [String] Mustache)

creates a Str.

template

defn

(Fn [(Ref String a), (Ref (Map String Mustache) b)] String)

                        (template s values)
                    

templates a Mustache-formatted string s using the map values.

The map must be from String to Mustache. Values can be:

  • Str — a plain string. Empty strings are falsy for section purposes.
  • Lst — an array of boxed Mustache values. Empty arrays are falsy; non-empty arrays cause sections to iterate once per item. Inside a list section, bare items are bound as {{.}} and Mp items provide nested object contexts.
  • Mp — a nested map of boxed Mustache values. Used as a section value, it pushes its entries onto the context for the section body.
  • Lambda — a function applied to the raw section body.

Example:

(Mustache.template
  "this is a super {{ adjective }} library"
  &{@"adjective" (Mustache.Str @"cool")})

Trimming: names inside mustache tags are trimmed, so {{ thing }} looks up "thing". Opening and closing section markers must use consistent names, i.e. {{/thing}} is not a close for {{^ thing }}.