问题
Have a look at my index.yaws file below
<html>
<body>
<h4>Data:</h4>
<erl>
out(Arg) ->
Data = utilities:get_raw_data(),
{html, io_lib:format("~p", [Data])}.
</erl>
<erl>
out(Arg) ->
Data = utilities:get_raw_data(),
lists:foreach(fun(X) -> {Id, Fname, Lname} = X, io:format("ID: ~p ", [Lname]) end, Data).
</erl>
</body>
</html>
The first part of the code runs correctly producing output such as
[{3,"Matt","Williamson3"}, {2,"Matt","Williamson2"}, {1,"Matt","Williamson"}]
There is no error on the second part, but the web page remains blank. I believe the section
io:format("ID: ~p ", [Lname])
doesn't print out to the browser.
What do I get to change in order for it to work?
回答1:
Try this instead of the foreach
line: (untested)
{html, lists:map(fun(X) -> {Id, Fname, Lname} = X, io_lib:format("ID: ~p ", [Lname]) end, Data)}.
That is, instead of printing using io:format
, return the data in a {html, Iodata}
tuple, as in the first <erl>
block.
来源:https://stackoverflow.com/questions/16439505/output-data-of-erlang-list-as-a-html-in-yaws