Example Code - simple case
people.html - renders data from the model
<html>
<head>
<title>Erlang Web Sample Page 4</title>
</head>
<body>
<center>
<wpart:people rows="2" key="list">
<tr>
<td><wpart:lookup key="item:person" /></td>
<td><wpart:lookup key="item:age" format="age"/></td>
<td><wpart:lookup key="item:sex" format="sex"/></td>
</tr>
</wpart:people>
<img src="/images/powered.gif" />
</center>
</body>
</html>
install.erl - initialises mnesia database and puts sample data into it
-module(install).
-export([install/0]).
install() ->
mnesia:create_schema([node()]),
application:start(mnesia),
mnesia:create_table(person,
[{disc_copies, [node()]},
{attributes, record_info(fields,person)}]),
mnesia:dirty_write(#person{name = "Lucy", age = "20", sex="female"}),
mnesia:dirty_write(#person{name = "John", age = "22", sex="male"}),
mnesia:dirty_write(#person{name = "Anna", age = "22", sex="female"}),
mod.erl - controller function writes data into the request dictionary
-module(mod).
-export([validate/1,func/0,]).
-export([install/0]).
-record(person, {name, age, sex}).
validate(func) ->
{ok, []};
func() ->
Keys = mnesia:dirty_all_keys(person),
Records = [mnesia:dirty_read(person, Key) || Key <- Keys],
Persons = lists:map(fun([#person{name=Name, age=Age, sex=Sex}]) ->
[{"person",Name},
{"age",Age},
{"sex",Sex}]
end,
Records),
eptic:fset("list", Persons),
template.
wpart_people.erl - wpart module handles formatting and retrieves data from the request dictionary
-module(wpart_people).
-export([handle_call/1]).
-include_lib("xmerl/include/xmerl.hrl").
handle_call(E) ->
Start = case catch list_to_integer(eptic:fget("get", "start")) of
S when is_integer(S) -> S;
_ -> 1
end,
Rows = case catch list_to_integer(wpart:has_attribute("attribute::rows", E)) of
R when is_integer(R) -> R;
_ -> 10
end,
Key = wpart:has_attribute("attribute::key", E),
List = eptic:fget(Key),
Prev = if Start-Rows > 0 -> link_prev(Start-Rows);
Start == 1 -> "Prev | ";
true -> link_prev(1)
end,
Next = if Start+Rows > length(List) -> "Next";
true -> link_next(Start + Rows)
end,
F = fun(Item, {N,Start,End,Acc}) when N>=Start, N<End ->
eptic:fset("item", Item),
{N+1,Start,End,[wpart:eval(E#xmlElement.content)|Acc]};
(_, {N,Start,End,Acc}) ->
{N+1,Start,End,Acc}
end,
{_,_,_,TableRows} = lists:foldl(F, {1,Start,Start+Rows,[]}, List),
[#xmlText{value = "<table>", type=cdata},
lists:reverse(TableRows),
#xmlText{value = "</table>", type=cdata},
#xmlText{value = Prev, type=cdata},
#xmlText{value = Next, type=cdata}].
link_prev(Start) ->
["<a href=\"?start=",integer_to_list(Start),"\">Prev</a> | "].
link_next(Start) ->
["<a href=\"?start=",integer_to_list(Start),"\">Next</a>"].