Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Rails view template for a gallery

Reply
Thread Tools

Rails view template for a gallery

 
 
stephen O'D
Guest
Posts: n/a
 
      08-01-2006
I am totally new to rails, so sorry for the trivial question!

I have an object, gallery, that contains a list of images. I want to
display these images in a table with 4 columns and as many rows as it
takes, closing the final table row at the end.

What is the best way todo this?

I can do it (mostly, the final table row is still left open) using the
following template, but it seems a bit hackish in places:

<html>
<body>
<h1><%= @gallery.name -%></h1>
<table>
<%
i = 1
for img in @gallery.images
-%>
<%= if i == 1 || (i-1)%4 == 0
"<tr>"
else
""
end -%>
<td><img src='<%= img -%>'></td>
<%= "</tr>" if i%4 == 0 -%>
<%= i += 1
"" -%>
<% end -%>
</table>
</body>
</html>

Is there a better way?

 
Reply With Quote
 
 
 
 
Justin Collins
Guest
Posts: n/a
 
      08-01-2006
stephen O'D wrote:
> I am totally new to rails, so sorry for the trivial question!
>
> I have an object, gallery, that contains a list of images. I want to
> display these images in a table with 4 columns and as many rows as it
> takes, closing the final table row at the end.
>
> What is the best way todo this?
>
> I can do it (mostly, the final table row is still left open) using the
> following template, but it seems a bit hackish in places:
>
> <html>
> <body>
> <h1><%= @gallery.name -%></h1>
> <table>
> <%
> i = 1
> for img in @gallery.images
> -%>
> <%= if i == 1 || (i-1)%4 == 0
> "<tr>"
> else
> ""
> end -%>
> <td><img src='<%= img -%>'></td>
> <%= "</tr>" if i%4 == 0 -%>
> <%= i += 1
> "" -%>
> <% end -%>
> </table>
> </body>
> </html>
>
> Is there a better way?
>

Generally, you'll want to ask Rails-specific questions on the Rails
mailing list, as this is the Ruby mailing list.

http://lists.rubyonrails.org/mailman/listinfo/rails


-Justin

 
Reply With Quote
 
 
 
 
Matt Todd
Guest
Posts: n/a
 
      08-02-2006
I'll be honest, I think this is Ruby-centric enough because it really
comes down to basic looping and Erb, so I'll take a stab at it!

<html>
<body>
<h1><%= @gallery.name -%></h1>
<table>
<% ((@gallery.length / 4) + (((@gallery.length % 4) > 0) ? 1 :
0)).downto(1) do %>
<tr>
<% 4.downto(1) do %>
<td><img src='<%= @gallery.shift -%>'></td>
<% end %>
</tr>
<% end %>
</table>
</body>
</html>

That basically figures out how many rows (based on the length divided
by 4, plus 1 if there were any leftovers) and then loops through them.
Then, for each row, we loop four times and for each iteration, we
shift off the front element to go from beginning to end (instead of
pop, which will pull if off the end).

I like this solution a bit better because it uses a very heirarchical
structure much like the way the data will be. Plus, I don't have to
embed any HTML into Erb!

Hope this helps,

M.T.

 
Reply With Quote
 
Brian Palmer
Guest
Posts: n/a
 
      08-02-2006
How about using Enumerator#each_slice (or Rails' #in_groups_of):

<html>
<body>
<h1><%= @gallery.name -%></h1>
<table>
<% @gallery.each_slice(4) do |slice| %>
<tr>
<% slice.each do |image| %>
<td><%= image_tag(image) %></td>
<% end %>
</tr>
<% end %>
</table>
</body>
</html>

-- Brian

On Aug 1, 2006, at 10:45 PM, Matt Todd wrote:

> I'll be honest, I think this is Ruby-centric enough because it really
> comes down to basic looping and Erb, so I'll take a stab at it!
>
> <html>
> <body>
> <h1><%= @gallery.name -%></h1>
> <table>
> <% ((@gallery.length / 4) + (((@gallery.length % 4) > 0) ? 1 :
> 0)).downto(1) do %>
> <tr>
> <% 4.downto(1) do %>
> <td><img src='<%= @gallery.shift -%>'></td>
> <% end %>
> </tr>
> <% end %>
> </table>
> </body>
> </html>
>
> That basically figures out how many rows (based on the length divided
> by 4, plus 1 if there were any leftovers) and then loops through them.
> Then, for each row, we loop four times and for each iteration, we
> shift off the front element to go from beginning to end (instead of
> pop, which will pull if off the end).
>
> I like this solution a bit better because it uses a very heirarchical
> structure much like the way the data will be. Plus, I don't have to
> embed any HTML into Erb!
>
> Hope this helps,
>
> M.T.
>



 
Reply With Quote
 
Matt Todd
Guest
Posts: n/a
 
      08-02-2006
I was writing to my local Ruby user group mailing list about it and it
dawned on me: I should just partition it! Brian beat me too it, though
(good thinking). I didn't find documentation on Enumerable#each_slice,
but I did find Array#slice which should do the same thing (though, I
just noticed the documentation was for 1.6, so that's probably my
problem).

But, in retrospect, I'd do what Brian did: it's short, readable, and
maybe faster? (I'd have to benchmark it.)

M.T.

 
Reply With Quote
 
stephen O'D
Guest
Posts: n/a
 
      08-02-2006

Matt Todd wrote:
> I was writing to my local Ruby user group mailing list about it and it
> dawned on me: I should just partition it! Brian beat me too it, though
> (good thinking). I didn't find documentation on Enumerable#each_slice,
> but I did find Array#slice which should do the same thing (though, I
> just noticed the documentation was for 1.6, so that's probably my
> problem).
>
> But, in retrospect, I'd do what Brian did: it's short, readable, and
> maybe faster? (I'd have to benchmark it.)
>
> M.T.


Guys thats great - I new there had to be a tidier way!

Can I also say thats about the most helpful response I have ever had on
usenet after posting in the wrong group!

>From now on I put rails questions in the rails group.


Cheers,

Stephen.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Rails suggestions/examples on Rails view architecture for spreadsheet-liketables? Kenneth McDonald Ruby 0 04-26-2007 07:51 PM
How to make a week view and day view calendar just like month view calendar in .NET ? Parthiv Joshi ASP .Net Web Controls 1 07-06-2004 03:15 PM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments