public
Created

  • Download Gist
gistfile1.haml
Haml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
- @last_date = ''
 
- @results.each do |row|
 
- if row.election_d != @last_date
- @last_date = row.election_d
%h2= row.election_d
%table
 
%tr
%td{ :style => "background-color: #{row.party_colour}" }
 
%td= row.party_name
%td= row.c_surname
%td= to_ordinal(row.position)
%td= commify(row.votes)

Haml issues in a Sinatra app. I'm trying to output a collection of tables using data from a single array. This kind of thing works fine in non-significant-whitespace templates but Haml (understandably) can't work out that the %h2 and %table are meant to enclose the following %trs in the block below. Any ideas for a workaround? I could generate each table from a separate query but it's preferable to do them all at once.

You have to nest the %tr inside the %table to work in haml..
You could consider some other approach like grouping the records by election_d before and then iterating.
Assiming you're on ruby 1.9, you could try something like

@results.group_by{|result| result.election_d }.each do |election_date, row|
  %h2= election_date
  %table
    %tr
      %td

Thanks for that. I hadn't heard of group_by before but I can see how that's useful.

In the end I simplified it by using nested loops around first the elections and then the candidacies for each election as in this commit.

Here it is in action.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.