| Author |
Message |
|
| Guest |
Posted: Wed Dec 12, 2007 12:54 am |
|
|
|
Guest
|
Hi list,
I really like erlyweb. Once of the problems I'm having with it is
that
some of the idioms required to do common things in the view require a
bit more erlang knowledge than I would like and it would be good if
this
could be abstracted for the common cases to allow designers without
erlang knowledge to still maintain the templates.
For example, to loop over a list the documentation suggests that I
define a function and pass the list off to that. I'd suggest a
simpler
interface for that would be a FOREACH loop, e.g. :
<% FOREACH {Title, Artist, Songs} = Data %>
Title: <b><% Title %></b><br>
Artist: <b><% Artist %></b><br>
<% FOREACH {Number, Name} = Songs %>
Song <% integer_to_list(Number) %> - <% Name %><br>
<% END %>
<% END %>
This is nothing new, of course, it is a familair construct from
templating systems in other languages and that is really the point,
i.e.
designers are familiar with it.
It would still work the same way under the hood, of course, with the
body of the FOREACH most likely being compiled to an erlang function,
however the interface IMO is nicer. I can have a go at putting
together
a patch if there is any interest.
Any thoughts?
Thanks.
--~--~---------~--~----~------------~-------~--~----~
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 |
|
|
| Back to top |
|
| Guest |
Posted: Wed Dec 12, 2007 1:47 am |
|
|
|
Guest
|
|
| Back to top |
|
| ketralnis |
Posted: Wed Dec 12, 2007 4:18 am |
|
|
|
User
Joined: 20 Jul 2007
Posts: 151
Location: San Francisco, CA
|
> I really like erlyweb. Once of the problems I'm having with it is
> that some of the idioms required to do common things in the view
> require a bit more erlang knowledge than I would like and it would
> be good if this could be abstracted for the common cases to allow
> designers without erlang knowledge to still maintain the templates.
Then people that don't want to learn Erlang will have to learn your
FOREACH construct. Then someone will come along asking for an easier
way to do your FOREACH construct, and so on.
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.
Erlyweb has a good component system. I recommend learning it, and
using it instead of trying to do much in the view. Yes, this means
learning some Erlang.
------------------------------
-module(artist_controller).
-compile(export_all).
h(Text) ->
sanitise_html().
show(A,ArtistName) ->
Artist=artist:find_name(ArtistName),
[ {data, h(ArtistName)},
[ {ewc, artist, album, [A,Album]}
|| Album <- artist:albums(Artist) ]].
album(A,Album) ->
Title=album:title(Album),
[ {data, h(Title) },
[ {ewc,artist,song,[A,Song]
|| Song <- album:songs(Album) ]].
song(A,Song) ->
[ {data,h(song:title(Song)) },
{data,h(song:lyrics(Song)) } ].
------------------------------
And the view:
------------------------------
<%@ show([ArtistName, Albums]) %>
<h1><% ArtistName %></h1>
<h2>Albums</h2>
<ul>
Albums
</ul>
<%@ album([Title, Songs]) %>
<li>
<% Title %>
<ul>
<% Songs %>
</ul>
</li>
<%@ song([Title,Lyrics]) %>
<li>
<h3><% Title %></h3>
<pre><% Lyrics %></pre>
</li>
------------------------------
With the appropriate models, that should do what you're looking for.
The "foreach" bits are the list comprehensions, like this:
[ Expr || Expr <- generator() ].
That could also be done with a map:
lists:map(fun(Expr) ->
Expr
end, generator()).
>
>
> For example, to loop over a list the documentation suggests that I
> define a function and pass the list off to that. I'd suggest a
> simpler
> interface for that would be a FOREACH loop, e.g. :
>
> <% FOREACH {Title, Artist, Songs} = Data %>
> Title: <b><% Title %></b><br>
> Artist: <b><% Artist %></b><br>
> <% FOREACH {Number, Name} = Songs %>
> Song <% integer_to_list(Number) %> - <% Name %><br>
> <% END %>
> <% END %>
>
> This is nothing new, of course, it is a familair construct from
> templating systems in other languages and that is really the point,
> i.e.
> designers are familiar with it.
>
> It would still work the same way under the hood, of course, with the
> body of the FOREACH most likely being compiled to an erlang function,
> however the interface IMO is nicer. I can have a go at putting
> together
> a patch if there is any interest.
>
> Any thoughts?
>
> Thanks.
>
--~--~---------~--~----~------------~-------~--~----~
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 |
|
|
| Back to top |
|
| Guest |
Posted: Wed Dec 12, 2007 5:52 am |
|
|
|
Guest
|
On Dec 11, 2007 5:47 PM, jm <jeffm@ghostgun.com> wrote:
>
> Colm wrote:
>
> > Any thoughts?
>
> lists:foreach()
>
> http://www.erlang.org/doc/man/lists.html#foreach/2
>
> Jeff.
Stricly speaking, you would have to use lists:map(), because
lists:foreach returns 'ok', and you want to return the actual iolist
in an ErlTL template expression.
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 |
|
|
| Back to top |
|
| Guest |
Posted: Wed Dec 12, 2007 6:25 am |
|
|
|
Guest
|
>
> 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 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 |
|
|
| Back to top |
|
| Guest |
Posted: Wed Dec 12, 2007 2:56 pm |
|
|
|
Guest
|
On Dec 12, 4:17 am, David King <dk...@ketralnis.com> wrote:
> > I really like erlyweb. Once of the problems I'm having with it is
> > that some of the idioms required to do common things in the view
> > require a bit more erlang knowledge than I would like and it would
> > be good if this could be abstracted for the common cases to allow
> > designers without erlang knowledge to still maintain the templates.
>
> Then people that don't want to learn Erlang will have to learn your
> FOREACH construct. Then someone will come along asking for an easier
> way to do your FOREACH construct, and so on.
I don't agree that a suggestion for a simpler idiom should be
immediately ruled out on the basis that people will want increasingly
simpler versions. The FOREACH is just an example to illustrate the
idea. I'm simply advocating a way to define inline chunks of HTML in
cases where you don't need a component because the output is not
generic enough to be reused. I think this is pretty common and it
would be nice if there was a macro for it. I'm not a designer - but I
work with designers who don't have a functional programming mindset
but they can instantly relate to foreach / if style constructs and
they prefer inline definitions for non-reusable output.
> 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.
>
> Erlyweb has a good component system. I recommend learning it, and
> using it instead of trying to do much in the view. Yes, this means
> learning some Erlang.
>....
Thanks. That is a nice example. I need to study it some more. I
fundamentally agree that the view should be as simple as possible with
little or no inline erlang code. I'm just saying it would be nice if
your example could have the compnents inlined in some way, I'm not
saying FOREACH is the right way to do it, I'm just thinking out loud
mostly. Your example is really elegant I'd just rather than the
controller didn't have to know that "song" and "album" components
exist in the view.
Thanks
--~--~---------~--~----~------------~-------~--~----~
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 |
|
|
| Back to top |
|
| Guest |
Posted: Wed Dec 12, 2007 3:03 pm |
|
|
|
Guest
|
> 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/>...
Cool. I hadn't read that but that is a great suggestion.
> 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 really like that idea. It solves a large part of what I was trying
to get at with the FOREACH suggestion.
> 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.
Sure. I don't think that is called for in this case, though. If you
are open to adding a few extensions like the <et:map then I think that
would simplify the common case which is all I'm trying to address.
Thanks.
--~--~---------~--~----~------------~-------~--~----~
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 |
|
|
| Back to top |
|
|
|
All times are GMT
|
|
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
|
|
|