On Jan 25, 2008 10:12 PM, Yariv Sadan <[EMAIL PROTECTED]> 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 :)

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 [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
-~----------~----~----~----~------~----~------~--~---

Reply via email to