Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I wrap multiline Ruby arguments in HAML?

How can I use multiple lines for a single Ruby statement in HAML? For example, I'd like to write something like the following:

- entries = [{
-   :title => "The Fellowship of the Ring", 
-   :body => "We should walk instead of fly"
- }]

!!! 5
%html
  %head
    %title Blog
  %body
    #content
      - entries.each do |entry|
        .entry
          %h3.title= entry[:title]
          %p.body= entry[:body]

But of course the first four lines produce a syntax error.

like image 365
jhchen Avatar asked Mar 24 '13 23:03

jhchen


3 Answers

Wrap Arguments with Commas

According to the HAML FAQ:

[W]hen a function has lots of arguments, it’s possible to wrap it across multiple lines as long as each line ends in a comma.

So, the following should be valid HAML:

- entries = [{ :title => "The Fellowship of the Ring",                 :body  => "We should walk instead of fly" }] 
like image 187
Todd A. Jacobs Avatar answered Sep 22 '22 17:09

Todd A. Jacobs


You can use :ruby filter.

:ruby   entries = [{      :title => "The Fellowship of the Ring",      :body  => "We should walk instead of fly"    }]  !!! 5 %html   %head     %title Blog   %body     #content       - entries.each do |entry|         .entry           %h3.title= entry[:title]           %p.body= entry[:body] 
like image 33
Satoshi Sachin Ohmori Avatar answered Sep 25 '22 17:09

Satoshi Sachin Ohmori


Check out the docs here.

Note that it's intentionally awkward because you're not supposed to be putting lots of ruby in your templates.

like image 40
Luke Avatar answered Sep 24 '22 17:09

Luke