A library to render Mustach templates.
It complies with the Mustache's manual and specs.
⚠️ While all major features are present, this library is still under development. The following have yet to be added:
- fetch template & partials from file
- inheritancy
> mustache:render("Hello {{wut}}!", #{ wut => "world" }).
"Hello world!"
> mustache:render(<<"Hello {{wut}}!">>, [{ wut, "world" }]).
"Hello world!"
The section Non-Empty Lists of the manual, states that a section should be displayed as many time as there's element in the list.
The problem is that erlang's strings are lists, which means that the following code will display as such:
> Data = #{ string => "hello" }.
> mustache:render("{{#string}}oups!{{/string}}", Data).
"oups!oups!oups!oups!oups!"
Use binary to avoid this issue:
> Data = #{ string => <<"hello">> }.
> mustache:render("{{#string}}ok{{/string}}", Data).
"ok"
> Fun = fun (Template) ->
% in here Template = "hello {{name}}",
"<strong>" ++ Template ++ "</strong>"
end.
> Map = #{ name => "Tom", lambda => Fun }.
> mustache:render("{{#lambda}}hello {{name}}{{/lambda}}", Map).
"<strong>hello Tom</strong>"
Tags that begin with a bang (!
) won't be displayed:
> mustache:render("Hello{{! won't be displayed }}!").
"Hello!"
Tags that begin with >
will include external template or "partials".
mustache:render
takes a third arguments for partials:
> Template = "ducks:\n{{#ducks}}{{>item}}{{/ducks}}".
> Map = #{ ducks => ["Huey", "Dewey", "Louie"] }.
> Partials = #{ item => "- {{.}}\n" }.
> mustache:render(Template, Map, Partials).
"ducks:\n- Huey\n- Dewey\n- Louie\n"
If not given to render
, Partials will be directly fetched from file
This feature is comming soon
This library complies with the following specs:
- comments
- delimiters
- dynamic-names
- inheritance
- interpolation
- inverted
- lambdas
- partials
- sections
This repo is under the MIT's license