wpart formatting rules
Having set some values within the request dictionary we are able to display them directly as they are on the site. Some problems may appear when the values are not a strings but tuples, integers or list of values. Then, instead of converting those values to the string format in the controller (before inserting them into the request dictionary) we could format them "on the fly" - during the template expanding process.
Syntax
The syntax of the formatters depends on the context.
format attribute
Using the <wpart:lookup format=Format key=Key /> allows us to access the value stored under some key in the request dictionary and format it with the formatter specified by Format:
Format = "formatting_module(arguments)"
During the template expanding, the value Val is fetched from request dictionary (it is stored under key Key) and the
wtype_formatting_module:handle_call("arguments", Val)is called.
The result is rendered on the site.
wpart attribute's namespace
We can also use wpart namespace in each of the standard HTML attribute:
<a wpart:href=Value ...>...</a>
If the attribute is in wpart namespace, its value will be expanded as well.
The part of the string that are inside the curly brackets { } specifies the key stored inside the request dictionary. The parts that are outside of the brackets are left as they were. For example, if we use
controller.erl
...
wpart:fset("text", "tree"),
{template, "index.html"}.
...
index.html
...
<img wpart:alt="This is a {text}!" wpart:src="/img/{text}.jpg" />
...But simple substitution is often not enough: we would like to format some values as well. In this case we should put the formatter definition before the Key (the format of the formatter is the same as in format attribute) and surround it with square brackets: [ ]:
wpart:attribute="Some text {[my_formatter(formatter_attribute)]key} and some text"There is a default printer implemented, that uses io_lib:format to print out the term: wtype_term. If the formatting arguments are passed they are used as an argument for io_lib:format call, otherwise, the ~w is used:
wpart:attribute="{[term()]key}"
wpart:attribute="{[term(~p)]key}"
Example
We would like to display the number in the different bases:
calculator.erl
...
wpart:fset("result", Number), %% Number :: integer()
wpart:fset("seq", lists:seq(2, 16)),
{template, "result.html"}.
...
wtype_base_converter.erl
...
handle_call(Base, N) when is_integer(N), is_list(Base) ->
case catch list_to_integer(Base) of
{'EXIT', _} ->
"incorrect base!: " ++ Base;
IntBase ->
erlang:integer_to_list(N, IntBase)
end.
...
result.html
...
<wpart:list select="map" list="seq" as="base">
Base: <wpart:lookup key="base" />,
result: <wpart:lookup key="result" wpart:format="base_converter({base})" />
<br/>
</wpart:list>
...