Hi Maddiin,
Thanks for writing this... it's so much better than my half baked
tutorial. It'll be very helpful for people who are starting to learn
ErlyWeb.
I have a few comments:
1) Instead of returning a list containing multiple {data, Data}
tuples, you can return a single tuple of the form {data, {Data1,
Data2, ....}}. It requires less typing.
2) Avoid calling integer_to_list() in view templates. Views should
expect the controllers to always pass into them iolists (with one
exception, that I'll get to shortly).
3) You can call Module:insert() instead of Module:save() if you don't
need to get the object's id. insert() is a bit more efficient in those
cases because it doesn't attempt to get the inserted id from the
database.
4) When you want to display the values of ErlyDB record fields in view
templates, you have two options. The first is to extract the field
values in the controller and pass them as a list to the view, e.g.:
controller:
entry(A, Entry) ->
[{data, entry:id(Entry)},
{data, entry:title(Entry)},
{data, entry:body(Entry)}
].
view:
<%@ entry([Id, Title, Body]) %>
<h3><a href="/entry/detail/<% integer_to_list(Id) %>"><% Title %></a></h3>
<p><% Body %></p>
An alternative way, which I think is nicer, is to pass a function as
in this example:
entry(A, Entry) ->
{data, get_field_fun(Entry)},
%% this function is generic -- you can use it for a record of any type.
get_field_fun(Rec) ->
Module = element(1, Rec),
fun(Field) ->
erlydb_base:field_to_iolist(Module:Field(Rec))
end.
<%@ entry(F) %>
<h3><a href="/entry/detail/<% F(id) %>"><% F(title) %></a></h3>
<p><% F(body) %></p>
The advantage of this approach is that you don't have to enumerate the
fields you want to display in both the controller and the view, and
the values are automatically converted to iolist in the view function.
Furthermore, you can easily change the fields you display in the view
without modifying the controller code. You can think of it as lazy,
type safe template evaluation :)
Finally, would you mind adding this tutorial to the ErlyWeb wiki on
Google code? I'd like all the user-contributed documentation to be
there so it's easier to find.
Thanks again!
Yariv
On Jan 25, 2008 7:03 PM, maddiin <[EMAIL PROTECTED]> wrote:
>
> Hi there,
>
> I added an introduction tutorial to the list. You´ll find it here:
> http://groups.google.com/group/erlyweb/web/getting-your-feet-wet-with-erlyweb
>
> Please check it for grammar and spelling mistakes and the content
> itself.
>
> I´ve also made a table of contents for a tutorial section I´d like to
> discuss with you:
>
> * Installation
> * Mac OS X
> * Windows
> * Getting your feet wet
> * Component System
> * Models
> * Controller
> * Views
> * Database Api
> * Creating records
> * Query-examples
> * Relations
> * etc
> * xyz_app_controller
> * Error handling
> * Phased vars
> * URL configuration
> * Form Processing
> * Validate forms
> * Preventing CSRF
> * Sessions
> * How to log a user in&out
> * Show welcome msg to logged in user
> * Caching
> * Syndication
> * rss, atom, etc
>
> Like suggested on the list, this could build on a blog application, so
> the form processing could be explained by adding comments to the blog
> application, many-to-many relations by adding tags to the blog...until
> every aspect of ErlyWeb is explained. I think its most valuable for
> new users if they can associate it with an application they are
> building throughout a tutorials section.
>
> I am curious about your suggestions and complaints. :)
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"erlyweb" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---