wpart_list
wpart_list provides you with the ability to manipulate lists. You can perform an operation on a list through iteration using "map." A list consists of two parts, the head and the tail. The head is an Erlang term, such as a number, tuple, atom, list, et cetera. And the tail is a list. The head and tail are separated with the "|" pipe character. The list is terminated when the tail is equal to an empty list.
Attributes
select
Type of the operation. Can be:
map - does a for each on the body, i.e. renders the body for all element it the list
head - renders the body for the first element in the list
tail - renders the body for the last element in the list
filter - filters the list according to the pred attribute
find - returns the first element of the list for which the pred attribute evaluates to true
sort - sorts the list according to the pred attribute
as
Inserts the content of the evaluation into the as dictionary key.
list
Specifies which list in dictionary will be traversed.
pred
Erlang function definition.
Example
1. Print out all the elements of the list:
map.html
...
<ul>
<wpart:list select="map" list="names" as="name">
<li><wpart:lookup key="name" /></li>
</wpart:list>
</ul>
...2. Print out first element of the list:
head.html
...
<wpart:list select="head" list="line" as="first">
First in line: <wpart:lookup key="first" />
</wpart:list>
...3. Access the last element of the list:
tail.html
...
<wpart:list select="tail" list="snake" as="piece">
<wpart:choose>
<wpart:when test="{piece} == rattle">
Aargh! It’s a rattle snake! Run!
</wpart:when>
<wpart:otherwise>
Ooh! Come here, cute snake!
</wpart:otherwise>
</wpart:choose>
</wpart:list>
...4. Filter some elements:
filter.html
...
<wpart:list select="filter" list="employees" as="cool_employees"
pred="fun(E) -> element(3, E) == cool end.">
The cool ones are:
<wpart:list select="map" list="cool_employees" as="ce">
<wpart:lookup key="ce" />,
</wpart:list>
</wpart:list>
...5. Find elements of the list:
find.html
...
<wpart:list select="find" list="everyone" as="the_chosen_one"
pred="fun(E) -> E == chosen end.">
<wpart:choose>
<wpart:when test="{the_chosen_one} == []">
We are all equal.
</wpart:when>
<wpart:otherwise>
Go cry, emo kid!
</wpart:otherwise>
</wpart:choose>
</wpart:list>
...6. Sort the list:
sort.html
...
<wpart:list select="sort" list="movies"
as="top_rated_movies" pred="fun(M1, M2) -> M1 > M2 end.">
The movie I liked most ever is
<wpart:list select="head" list="top_rated_movies" as="best_movie">
<wpart:lookup key="best_movie" />
</wpart:list>
</wpart:list>
...The strings inside the { } will be used as the keys to the request dictionary: their corresponding values will be inserted in their places.
