Skip to content

Commit

Permalink
Merge branch 'improved_tests'
Browse files Browse the repository at this point in the history
  • Loading branch information
Eptwalabha committed Nov 21, 2022
2 parents fbb5574 + b29ccd3 commit 4b015a0
Show file tree
Hide file tree
Showing 18 changed files with 2,389 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ _build
rebar3.crashdump
*~
.*.swp
*.tar.gz
50 changes: 33 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# mustache

[![Erlang CI](https://github.com/Eptwalabha/mustache/actions/workflows/erlang.yml/badge.svg)](https://github.com/Eptwalabha/mustache/actions/workflows/erlang.yml)
[![Erlang CI](https://github.com/Eptwalabha/mustache/actions/workflows/erlang.yml/badge.svg)](https://github.com/Eptwalabha/mustache/actions/workflows/erlang.yml)
Current version: `v0.2.0`

A library to render [Mustach](https://mustache.github.io/) templates.
It tries to comply with the [Mustache's manual](https://mustache.github.io/mustache.5.html) as close as possible.
It complies with the [Mustache's manual](https://mustache.github.io/mustache.5.html) and [specs](#specs).

> :warning: While all major features are present, this library is still under development. The following have yet to be added:
> - fetch template & partials from file
> - allow [different delimiter](https://mustache.github.io/mustache.5.html#Set-Delimiter)
> - inheritancy
## Usage

Expand All @@ -23,20 +24,22 @@ It tries to comply with the [Mustache's manual](https://mustache.github.io/musta
The section **Non-Empty Lists** of the [manual](https://mustache.github.io/mustache.5.html#Sections), 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](https://learnyousomeerlang.com/starting-out-for-real#highlighter_829076), which means that the following code will display as such:
``` erlang
> mustache:render("{{#string}}oups!{{/string}}", #{ string => "hello" }).
> Data = #{ string => "hello" }.
> mustache:render("{{#string}}oups!{{/string}}", Data).
"oups!oups!oups!oups!oups!"
```
To avoid this kind of problem, use binary instead:
Use binary to avoid this issue:
``` erlang
> mustache:render("{{#string}}ok{{/string}}", #{ string => <<"hello">> }).
> Data = #{ string => <<"hello">> }.
> mustache:render("{{#string}}ok{{/string}}", Data).
"ok"
```

## Sections and Lambdas
``` erlang
> Fun = fun (Template, Render) ->
New_template = "<strong>" ++ Template ++ "</strong>",
Render(New_template)
> Fun = fun (Template) ->
% in here Template = "hello {{name}}",
"<strong>" ++ Template ++ "</strong>"
end.
> Map = #{ name => "Tom", lambda => Fun }.
> mustache:render("{{#lambda}}hello {{name}}{{/lambda}}", Map).
Expand All @@ -51,20 +54,33 @@ Tags that begin with a bang (`!`) won't be displayed:
```

## Partials
Tags that begin with `>` will include external template.
Tags that begin with `>` will include external template or "partials".

### Using `Map`
Partial can be stored in the Parameters used to render the template like so:
`mustache:render` takes a third arguments for partials:
``` erlang
> Map = #{ ducks => ["Huey", "Dewey", "Louie"],
item => "- {{.}}\n" }.
> Template = "ducks:\n{{#ducks}}{{>item}}{{/ducks}}".
> mustache:render(Template, Map).
> Map = #{ ducks => ["Huey", "Dewey", "Louie"] }.
> Partials = #{ item => "- {{.}}\n" }.
> mustache:render(Template, Map, Partials).
"ducks:\n- Huey\n- Dewey\n- Louie\n"
```

### Using files
> Comming soon
### 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:

- [x] comments
- [x] delimiters
- [x] dynamic-names
- [ ] inheritance
- [x] interpolation
- [x] inverted
- [x] lambdas
- [x] partials
- [x] sections

## Copyright

Expand Down
2 changes: 1 addition & 1 deletion elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
ruleset => elvis_config },

#{ dirs => ["src", "test"],
filter => "*.erl",
filter => "mustache*.erl",
ruleset => erl_files,
rules => [{elvis_text_style, line_length, #{ limit => 80,
skip_comments => false }},
Expand Down
2 changes: 2 additions & 0 deletions include/mustache.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-define(FLAT(List), lists:flatten(List)).
-define(REV(List), lists:reverse(List)).
Loading

0 comments on commit 4b015a0

Please sign in to comment.