25

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.

4

5 に答える 5

42

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" }]
于 2013-03-24T23:27:57.297 に答える
9

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]
于 2018-02-19T23:42:10.210 に答える
7

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.

于 2013-03-24T23:21:26.563 に答える
2
%div= "First line " + |
      "and second line" |

Will render:

<div>First line and second line</div>

You can use the command-tool haml, for instance copy the code then pbpaste | haml on macOS, or with a file cat file.haml | haml.

If you don't even want a tag:

First line |
and second line |

Will produce:

First line and second line
于 2017-05-25T22:45:18.823 に答える
0

This works:

= link_to( |
  'Delete', |
  attachment_path(attachment),
  confirm: 'Are you sure?',
  method: :delete,
  remote: true,
  )

Note the pipes after both the first and second lines, and a space before the pipes!

于 2020-10-11T17:52:59.630 に答える