mustache

Mustache logic-less templates in OCaml
Library mustache
type loc = {
loc_start : Lexing.position;
loc_end : Lexing.position;
}
type desc =
| String of string
| Escaped of dotted_name
| Section of section
| Unescaped of dotted_name
| Partial of partial
| Inverted_section of section
| Concat of t list
| Comment of string
and section = {
name : dotted_name;
contents : t;
}
and partial = {
indent : int;
name : name;
contents : t option Lazy.t;
}
and t = {
loc : loc;
desc : desc;
}
val dummy_loc : loc

A value of type loc, guaranteed to be different from any valid location.

val parse_lx : Lexing.lexbuf -> t

Read

val of_string : string -> t
val pp : Format.formatter -> t -> unit

pp fmt template print a template as raw mustache to the formatter fmt.

val to_formatter : Format.formatter -> t -> unit

Alias for compatibility

val to_string : t -> string

to_string template uses to_formatter in order to return a string representing the template as raw mustache.

val render_fmt : ?strict:bool -> ?partials:( name -> t option ) -> Format.formatter -> t -> Json.t -> unit

render_fmt fmt template json render template, filling it with data from json, printing it to formatter fmt.

For each partial p, if partials p is Some t then the partial is substituted by t. Otherwise, the partial is substituted by the empty string is strict is false. If strict is true, the Missing_partial exception is raised.

val render : ?strict:bool -> ?partials:( name -> t option ) -> t -> Json.t -> string

render template json use render_fmt to render template with data from json and returns the resulting string.

val fold : string:( loc:loc -> string -> 'a ) -> section:( loc:loc -> inverted:bool -> dotted_name -> 'a -> 'a ) -> escaped:( loc:loc -> dotted_name -> 'a ) -> unescaped:( loc:loc -> dotted_name -> 'a ) -> partial:( loc:loc -> int -> name -> t option Lazy.t -> 'a ) -> comment:( loc:loc -> string -> 'a ) -> concat:( loc:loc -> 'a list -> 'a ) -> t -> 'a

fold template is the composition of f over parts of template, called in order of occurrence, where each f is one of the labelled arguments applied to the corresponding part. The default for f is the identity function.

  • parameter string

    Applied to each literal part of the template.

  • parameter escaped

    Applied to "name" for occurrences of {{name}}.

  • parameter unescaped

    Applied to "name" for occurrences of {{{name}}}.

  • parameter partial

    Applied to "box" for occurrences of {{> box}}.

  • parameter comment

    Applied to "comment" for occurrences of {{! comment}}.

val expand_partials : ( name -> t option ) -> t -> t

expand_partials f template is template where for each Partial p node, p.contents now evaluates to f p.name if they were evaluating to None. Note that no lazy is forced at this point, and calls to f are delayed until p.contents is forced.

module Infix : sig ... end

Shortcut for concatening two templates pieces.

val raw : loc:loc -> string -> t

<p>This is raw text.</p>

val escaped : loc:loc -> dotted_name -> t

{{name}}

val unescaped : loc:loc -> dotted_name -> t

{{{name}}}

val inverted_section : loc:loc -> dotted_name -> t -> t

{{^person}} {{/person}}

val section : loc:loc -> dotted_name -> t -> t

{{#person}} {{/person}}

val partial : loc:loc -> ?indent:int -> name -> t option Lazy.t -> t

{{> box}}

val comment : loc:loc -> string -> t

{{! this is a comment}}

val concat : loc:loc -> t list -> t

Group a t list as a single t.