How do I run a long block of ruby code in a HAML file, including an array?

246 views Asked by At

I have a bit of Ruby that outputs correctly as a .rb file. However, I want to include equivalent code in a HAML file, and I just can't get it to output correctly. I've tried to reformat it in various ways, but haven't been able to figure it out, nor find exactly what I'm looking for in another question.

I'm generating repeated div blocks, using elements stored in an array of arrays. There are groups of 3 .class objects that I want to group under a .row. I've got to make a BUNCH of these, so I want to automate this if possible. Right now I can run the ruby code in sublime text, and then copy+paste the formatted HAML into my HAML page... but it seems like I should be able to do it in-line!

Here's the Ruby code:

gfys = [
  [ #ROW 1
    {"Title"=>"MOVE", "ID"=>"#gfy0a", "Desc"=>"Use either analog stick or directional buttons."},
    {"Title"=>"SHOOT", "ID"=>"#gfy0b", "Desc"=>"Press any button; pull any trigger."},
    {"Title"=>"PUSH", "ID"=>"#gfy0c", "Desc"=>"Projectiles displace the ball on impact."}
  ],
  [ #ROW 2
    {"Title"=>"AIM", "ID"=>"#gfy1a", "Desc"=>"Projectiles are triangular. Balls are circular."},
    {"Title"=>"LAUNCH", "ID"=>"#gfy1b", "Desc"=>"Shoot while moving for faster projectiles."},
    {"Title"=>"SCORE", "ID"=>"#gfy1c", "Desc"=>"Push a ball into the opposing goal to earn a point."}
  ]
]

gfys.each do |group|
  puts "// ROW #{gfys.index(group)}"
  puts ".row"
  group.each do |gfy|
    puts "\t.class
    .class2
      %h2
        #{gfy["Title"]}
      #{gfy["ID"]}.class
      %p
        #{gfy["Desc"]}"
  end
end

This outputs correctly to the HAML code:

// ROW 0
.row
  .class
    .class2
      %h2
        MOVE
      #gfy0a.class
      %p
        Use either analog stick or directional buttons.
  .class
    .class2
      %h2
        SHOOT
      #gfy0b.class
      %p
        Press any button; pull any trigger.
  .class
    .class2
      %h2
        PUSH
      #gfy0c.class
      %p
        Projectiles displace the ball on impact.
// ROW 1
.row
  .class
    .class2
      %h2
        AIM
      #gfy1a.class
      %p
        Projectiles are triangular. Balls are circular.
  .class
    .class2
      %h2
        LAUNCH
      #gfy1b.class
      %p
        Shoot while moving for faster projectiles.
  .class
    .class2
      %h2
        SCORE
      #gfy1c.class
      %p
        Push a ball into the opposing goal to earn a point.

Now, just running the code under a :ruby filter in the HAML file doesn't work for me. It seems to print the whole array into the page, and then everything else gets goofy. Taking out the END tags, and accounting for HAML's attempts to "simplify" the code don't work. Putting - or = characters as appropriate before the code hasn't worked for me.

I know the situation is complicated by my formatting in the array, but collapsing the array to a single line didn't help, either.

Finally, I'm pre-compiling this before uploading it to a page, using Hammer.app, if that matters.

Is what I'm trying crazy? Or what am I missing? I really appreciate your help!

0

There are 0 answers