>
> IMHO, I'm afraid you'll just have to learn the idioms. Your best bet
> is to not do this in the view at all. In fact, I don't recommend
> passing artist objects into the views at all, only the viewable text
> itself.

This is true in general, but sometimes I also sometimes like passing
functions to views. I wrote about it here:
http://yarivsblog.com/articles/2007/02/23/erlyweb-tutorial-life-in-the-intersection-of-fp-and-dynamic-html/.
This technique is useful when you have a model with a lot of different
fields that you want to pass to the view. If you only passed the
strings/binaries, you would have to specify each field twice -- once
in the controller and once in the view, e.g.

controller:

show(A, Id) ->
  Artist = artist:find_id(Id),
  {data, [val_to_iolist(artist:F(Artist)) || F <- [name, age, country,
style, instrument, ....']}.

view:

<%@ show([Name, Age, Country, Style...]) %>
name: <% Name %><br/>
age: <% Age %><br/>...


You can save some typing by passing a Fun, e.g.

controller:

show(A, Id) ->
  Artist = artist:find_id(Id),
  {data, fun(Field) -> val_to_iolist(artist:Field(Artist)) end}.

view:

<%@ show(F) %>
name: <% F(name) %><br/>
age: <% F(age) %></br/>...


As you can see, this helps you defer declaring the list of field names
until you actually use those fields in the view. With a bit of work
you can make that fun generic and reuse it between components. I use
this style Vimagi and I really like it.

Going back to Colm's original suggestion, although I'm not sure that
adding that kind of FOREACH construct would make ErlTL accessible to
designers, I do agree that it would be nice to have a way of declaring
"anonymous functions" in the body of ErlTL functions so you don't have
to declare everything at the top level. Currently, ErlTL has syntax
for module-level functions, but not for funs. Such syntax would make
it possible to inline list comprehensions in a way that would
sometimes make them more readable. For example, this is what you would
do today:

<%@ album(Name, Songs, Comments) %>
Album: <% Name %>
<table>
<% [song(S) || S <- Songs %>
</table>
<% Comments %>

<%@ song(S) %><tr><td><% S %></td></tr>

The 'song' snipped is declared outside of where it is placed in the
body of the page. It would be nice to be able to inline it such as in


<%@ album(Name, Songs, Comments) %>
Album: <% Name %>
<table>
<et:map S <- Songs>
  <tr><td><% S %></td></tr>
 </et:map>
</table>
<% Comments %>

I'm not sure if that's the right syntax, but you get the idea.

I think this kind of feature would make ErlTL templates more
Erlang-friendly. If you want to make your templates truly
designer-friendly, you might be better off using a different, more
"regular" template language altogether. ErlyWeb doesn't strictly
require ErlTL and in fact it would be easy to support other template
languages. If anyone wants to implement more template languages that
are designer friendly (or the suggested inline map feature above),
I'll be happy to add them to the framework.

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