Erlang/OTP Forums

Author Message

<  Erlyweb mailing list  ~  ErlyWeb documentation outline

Guest
Posted: Sat Jan 26, 2008 3:04 am Reply with quote
Guest
Hi there,

I added an introduction tutorial to the list. You
Guest
Posted: Sat Jan 26, 2008 6:13 am Reply with quote
Guest
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 Smile

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 <maddiin@googlemail.com> wrote:
>
> Hi there,
>
> I added an introduction tutorial to the list. You
Guest
Posted: Sat Jan 26, 2008 6:21 am Reply with quote
Guest
On Jan 25, 2008 10:12 PM, Yariv Sadan <yarivsadan@gmail.com> wrote:
> 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 Smile

Btw, I forgot to mention that with this approach you can add
"psuedo-fields" to your records. For example, your controller can
return

GetFieldsFun = get_fields_fun(Entry),
{data,
fun(summary) ->
<<Summary/100, Rest>> = GetFieldsFun(body),
Summary;
(Field) ->
get_fields_fun(Field)
end}.

What's happening here? If the template code passes the 'summary' atom
to the function, as in <% F(summary) %>, we return the first 100 bytes
of the body value, otherwise we pass the parameter to the function to
get the default behavior.

Yariv

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
dmitriid
Posted: Sat Jan 26, 2008 10:53 am Reply with quote
User Joined: 17 Aug 2006 Posts: 213
> * Installation
> * Mac OS X
> * Windows
>
I'd also include Linux and FreeBSD

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "erlyweb" group.
To post to this group, send email to erlyweb@googlegroups.com
To unsubscribe from this group, send email to erlyweb-unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/erlyweb?hl=en
-~----------~----~----~----~------~----~------~--~---

Post recived from mailinglist
View user's profile Send private message
Guest
Posted: Sun Jan 27, 2008 12:50 am Reply with quote
Guest
> I'd also include Linux and FreeBSD

Of course, I put them on the list.

> 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.

I will also edit the tutorial taking care these points. Thanks for the
explanation.

>An alternative way, which I think is nicer, is to pass a function as
> in this example:

That

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum