Skip to content

Eptwalabha/mustache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mustache

Erlang CI
Current version: v0.2.0

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

Usage

> mustache:render("Hello {{wut}}!", #{ wut => "world" }).
"Hello world!"
> mustache:render(<<"Hello {{wut}}!">>, [{ wut, "world" }]).
"Hello world!"

Sections and non-empty lists (and Strings)

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"

Sections and Lambdas

> 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>"

Comments

Tags that begin with a bang (!) won't be displayed:

> mustache:render("Hello{{! won't be displayed }}!").
"Hello!"

Partials

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"

From file

If not given to render, Partials will be directly fetched from file

This feature is comming soon

Specs

This library complies with the following specs:

  • comments
  • delimiters
  • dynamic-names
  • inheritance
  • interpolation
  • inverted
  • lambdas
  • partials
  • sections

Copyright

This repo is under the MIT's license

About

render Mustache template with Erlang

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages