package mustache

  1. Overview
  2. Docs

Module MustacheSource

A module for creating and rendering mustache templates in OCaml.

Sourcemodule Json : sig ... end
Sourcetype loc = {
  1. loc_start : Lexing.position;
  2. loc_end : Lexing.position;
}
Sourcetype name = string
Sourcetype dotted_name = string list
Sourcetype t = {
  1. loc : loc;
  2. desc : desc;
}
Sourceand desc =
  1. | String of string
  2. | Escaped of dotted_name
  3. | Unescaped of dotted_name
  4. | Section of section
  5. | Inverted_section of section
  6. | Partial of partial
  7. | Param of param
  8. | Concat of t list
  9. | Comment of string
Sourceand section = {
  1. name : dotted_name;
  2. contents : t;
}
Sourceand partial = {
  1. indent : int;
  2. name : name;
  3. params : param list option;
  4. contents : t option Lazy.t;
}
Sourceand param = {
  1. indent : int;
  2. name : name;
  3. contents : t;
}
Sourceval pp_loc : Format.formatter -> loc -> unit
Sourcetype template_parse_error

Read template files; those function may raise Parse_error.

Sourceexception Parse_error of template_parse_error
Sourceval parse_lx : Lexing.lexbuf -> t
Sourceval of_string : string -> t
Sourceval pp_template_parse_error : Format.formatter -> template_parse_error -> unit

pp_template_parse_error fmt err prints a human-readable description of a template parse error on the given formatter. The function does not flush the formatter (in case you want to use it within boxes), so you should remember to do it yourself.

| try ignore (Mustache.of_string "{{!") with Mustache.Parse_error err -> Format.eprintf "%a@." Mustache.pp_template_parse_error err |

Sourceval pp : Format.formatter -> t -> unit

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

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

Alias for compatibility

Sourceval to_string : t -> string

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

Sourcetype render_error_kind =
  1. | Invalid_param of {
    1. name : dotted_name;
    2. expected_form : string;
    }
  2. | Missing_variable of {
    1. name : dotted_name;
    }
  3. | Missing_section of {
    1. name : dotted_name;
    }
  4. | Missing_partial of {
    1. name : name;
    }

Render templates; those functions may raise Render_error.

Sourcetype render_error = {
  1. loc : loc;
  2. kind : render_error_kind;
}
Sourceexception Render_error of render_error
Sourceval pp_render_error : Format.formatter -> render_error -> unit
Sourceval render_fmt : ?strict:bool -> ?partials:(name -> t option) -> Format.formatter -> t -> Json.t -> unit

render_fmt fmt template json renders 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, a Missing_partial error is raised.

  • raises Render_error

    when there is a mismatch between the template and the data. The Missing_* cases are only raised in strict mode, when strict is true.

Sourceval render_buf : ?strict:bool -> ?partials:(name -> t option) -> Buffer.t -> t -> Json.t -> unit

render_buf buf template json renders template, filling it with data from json, printing it to the buffer buf. See render_fmt.

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

render template json renders template, filling it with data from json, and returns the resulting string. See render_fmt.

Sourceval fold : string:(string -> 'a) -> section:(inverted:bool -> dotted_name -> 'a -> 'a) -> escaped:(dotted_name -> 'a) -> unescaped:(dotted_name -> 'a) -> partial: (?indent:int -> name -> ?params:(int * name * 'a) list -> t option Lazy.t -> 'a) -> param:(?indent:int -> name -> 'a -> '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}} or {{< box}}.

  • parameter params

    Applied to "param" for occurrences of {{$ param}}.

  • parameter comment

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

Sourceval 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.

Sourcemodule Infix : sig ... end

Shortcut for concatening two templates pieces.

Sourceval escape_html : string -> string

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

Sourceval raw : string -> t

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

Sourceval escaped : dotted_name -> t

{{name}}

Sourceval unescaped : dotted_name -> t

{{{name}}}

Sourceval inverted_section : dotted_name -> t -> t

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

Sourceval section : dotted_name -> t -> t

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

Sourceval partial : ?indent:int -> name -> ?params:(int * name * t) list -> t option Lazy.t -> t

{{> box}} or

  {{< box}}
    {{$param1}} default value for param1 {{/param1}}
    {{$param2}} default value for param1 {{/param2}}
  {{/box}}
Sourceval param : ?indent:int -> name -> t -> t

{{$foo}} {{/foo}}

Sourceval comment : string -> t

{{! this is a comment}}

Sourceval concat : t list -> t

Group a t list as a single t.

Sourcemodule With_locations : sig ... end

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

Sourceval erase_locs : With_locations.t -> t

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

Sourceval 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.