Skip to content

Instantly share code, notes, and snippets.

@mojombo
Created December 23, 2009 07:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mojombo/262380 to your computer and use it in GitHub Desktop.
Save mojombo/262380 to your computer and use it in GitHub Desktop.
Another mustache.erl example, this time showing off lists and asking for context in one of the view functions.
-module(complex).
-compile(export_all).
header() ->
"Colors".
item() ->
A = dict:from_list([{name, "red"}, {current, true}, {url, "#Red"}]),
B = dict:from_list([{name, "green"}, {current, false}, {url, "#Green"}]),
C = dict:from_list([{name, "blue"}, {current, false}, {url, "#Blue"}]),
[A, B, C].
link(Ctx) ->
{ok, Val} = dict:find(current, Ctx), Val.
list() ->
length(item()) =/= 0.
empty() ->
length(item()) =:= 0.
%%---------------------------------------------------------------------------
start() ->
code:add_patha(".."),
Output = mustache:render(complex, "complex.mustache"),
io:format(Output, []).
<h1>{{header}}</h1>
{{#list}}
<ul>
{{#item}}
{{#current}}
<li><strong>{{name}}</strong></li>
{{/current}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/item}}
</ul>
{{/list}}
{{#empty}}
<p>The list is empty.</p>
{{/empty}}
<h1>Colors</h1>
<ul>
<li><strong>red</strong></li>
<li><a href="#Red">red</a></li>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment