Search code examples
rubyhamlword-wrap

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.


Solution

  • 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" }]