20

使用(故意)奇怪的 HAML 多行格式,我想在我的模板中包含以下几行:

= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |

-# and

= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |

但是,它们不能相互碰撞,或者它们被读取为一个单独的多行块。

-# This fails:
= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |
= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |

有趣的是,用换行符分隔并没有更好的效果:

-# This fails, too:
= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |

= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |

我发现的唯一可行的解​​决方案是在两者之间运行一个空的 Ruby 代码行。这看起来真的很难看。

= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |
-
= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |

有更好的吗?

4

4 回答 4

28

这是一个功能,而不是一个错误。Haml 多行代码块故意笨拙——包括难以一个接一个地跟进——因为几乎所有时间都最好将 Ruby 代码放入帮助程序中。即使只调用一次帮助程序,它也会使您的模板更易于阅读。例如:

def blatz_link
  call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3',
    :foo4 => 'bar4', :foo5 => 'bar5'
end

def blootz_link
  call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3',
    :foo4 => 'bar4', :foo5 => 'bar5'
end

然后在你的 Haml 中,做

= blatz_link
= blootz_link

这将更具可读性和更容易理解。


如果您绝对必须在一个多行块后面跟着另一个,只需在其间添加注释:

= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |
-#
= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |
于 2009-09-25T03:25:22.543 回答
9

我遇到了与这里提到的相同的问题和解决方法,并且 HAML 关于多行块的奇怪(是的,这很奇怪)行为让我痛苦了好几次。我知道这是故意的,并且可能是为了强迫用户使他的代码更易于阅读。然而,众所周知的事实是,每个开发人员在构建代码时都有自己的偏好。HAML 是我所知道的唯一一种试图施加此类限制的语言(c、c++、ruby、python、HTML 等)。

将奇怪的多行处理称为功能而不是错误,仅表明存在缺陷的语言设计。最终它在用户眼中永远是一个错误。多行支持是任何主流语言的基本功能,缺少此功能只是很烦人 - 就像 M$ 回形针一样,我相信这也是一种引导用户的尝试。

话虽如此,HAML 是一种非常紧凑且有用的 HTML 编写语言。我们这些(在某些情况下)喜欢多行代码块的人会喜欢至少提供某种配置选项来启用/禁用体面的多行代码块支持——不管语言设计者对“易于阅读的代码”的个人定义如何”。

在我们到达那里之前,我想我们将不得不使用“-#”hack 来解决该语言......

于 2009-11-21T22:22:26.793 回答
3

你可以在你的助手上使用一个块,产生任何有意义的东西。

module SomeHelper
  def call_to_helper
    foo = Foo.new
    yield foo
    # build your html here, using the foo object's attributes
  end

  class Foo
    attr_accessor :foo1, :foo2, :foo3, :foo4, :foo5
  end

end

现在在你的haml上:

= call_to_helper do |foo|
  -foo.foo1 = 'bar1'
  -foo.foo2 = 'bar2'
  -foo.foo3 = 'bar3'
  -foo.foo4 = 'bar4'
  -foo.foo5 = 'bar5'

= call_to_helper do |foo|
  -foo.foo1 = 'bar1'
  -foo.foo2 = 'bar2'
  -foo.foo3 = 'bar3'
  -foo.foo4 = 'bar4'
  -foo.foo5 = 'bar5'
于 2009-09-24T22:27:01.210 回答
2

这是一种 hack(有点),但您始终可以在链中的第 2、第 3 等行上使用“+”而不是“=”。

= call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |
+ call_to_helper :foo1 => 'bar1', :foo2 => 'bar2', :foo3 => 'bar3', |
  :foo4 => 'bar4', :foo5 => 'bar5' |
于 2009-09-24T22:16:22.933 回答