package mustache

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
exception Invalid_param of string

A module for creating and rendering mustache templates in OCaml.

exception Invalid_template of string
exception Missing_variable of string

Raised when a missing variable in a template is not substituted

exception Missing_section of string
module Json : sig ... end
type t =
  1. | String of string
  2. | Escaped of string
  3. | Section of section
  4. | Unescaped of string
  5. | Partial of string
  6. | Inverted_section of section
  7. | Concat of t list
  8. | Comment of string
and section = {
  1. name : string;
  2. contents : t;
}
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 -> Format.formatter -> t -> Json.t -> unit

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

val render : ?strict:bool -> 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:(string -> 'a) -> section:(inverted:bool -> string -> 'a -> 'a) -> escaped:(string -> 'a) -> unescaped:(string -> 'a) -> partial:(string -> 'a) -> comment:(string -> 'a) -> concat:('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 : (string -> t) -> t -> t

expand_partials f template is template with f p substituted for each partial p.

module Infix : sig ... end

Shortcut for concatening two templates pieces.

val escape_html : string -> string

Escape &, "\"", ', < and > character for html rendering.

val raw : string -> t

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

val escaped : string -> t

{{name}}

val unescaped : string -> t

{{{name}}}

val inverted_section : string -> t -> t

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

val section : string -> t -> t

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

val partial : string -> t

{{> box}}

val comment : string -> t

{{! this is a comment}}

val concat : t list -> t

Group a t list as a single t.

module With_locations : sig ... end

Variant of the t mustache datatype which includes source-file locations, and associated functions.

val erase_locs : With_locations.t -> t

Erase locations from a mustache value of type With_locations.t.

val add_dummy_locs : t -> With_locations.t

Add the dummy_loc location to each node of a mustache value of type t.

OCaml

Innovation. Community. Security.