如何在HAML中包装多行Ruby参数?

jhc*_*hen 19 ruby haml word-wrap

如何在HAML中为单个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]
Run Code Online (Sandbox Code Playgroud)

但当然前四行会产生语法错误.

Tod*_*obs 37

用逗号包装参数

根据HAML常见问题解答:

[W]如果函数有很多参数,只要每行以逗号结尾,就可以将它包装在多行中.

所以,以下应该是有效的HAML:

- entries = [{ :title => "The Fellowship of the Ring", 
               :body  => "We should walk instead of fly" }]
Run Code Online (Sandbox Code Playgroud)


Luk*_*uke 6

这里查看文档.

请注意,它故意尴尬,因为你不应该在你的模板中放入大量的红宝石.


小智 5

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]
Run Code Online (Sandbox Code Playgroud)