Ruby-on-rails 多个多行 HAML 块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1474418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple multi-line HAML blocks
提问by Matchu
Using the (intentionally) strange multi-line format for HAML, I'd like to have the following lines in my template:
使用(故意)奇怪的 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' |
However, they can not run up against one another, or they are read as one single multi-line block.
但是,它们不能相互运行,或者它们被视为一个单一的多行块。
-# 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' |
And separating with a line break, interestingly enough, does no better:
有趣的是,用换行符分隔并没有更好的效果:
-# 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' |
The only working solution I have found is to run a blank line of Ruby code between. Which looks really ugly.
我发现的唯一可行的解决方案是在两者之间运行空白行的 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' |
Is there anything better?
有更好的吗?
回答by Natalie Weizenbaum
This is a feature, not a bug. Haml multiline blocks are intentionally unwieldy - including hard to follow one after another - because almost all the time it's better to put that Ruby code into a helper. Even if the helper is only called once, it will make your template much easier to read. For instance:
这是一个功能,而不是一个错误。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
Then in your Haml, just do
然后在你的哈姆尔,就做
= blatz_link
= blootz_link
which will be much more readable and easier to understand.
这将更具可读性和更容易理解。
If you absolutely must follow one multiline block with another, just add a comment in between:
如果您绝对必须在一个多行块后面跟随另一个,只需在其间添加注释:
= 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' |
回答by Jesper
I've encountered the same problems and workarounds as have been mentioned here, and the weird (and yes, it IS weird) behavior of HAML with regard to multi-line blocks has bitten me quite a few times. I know that it's intentional and that it was probably intended to force the user to make his code easier to read. However, it's a well known fact that every developer has his own preferences when it comes to structuring code. HAML is the only language I know of (c, c++, ruby, python, HTML, etc.) that tries to impose such restrictions.
我遇到了与此处提到的相同的问题和解决方法,并且 HAML 对多行块的奇怪(是的,这很奇怪)行为已经让我咬了很多次。我知道这是故意的,可能是为了迫使用户让他的代码更容易阅读。但是,众所周知,每个开发人员在构建代码时都有自己的偏好。HAML 是我所知道的唯一一种试图强加此类限制的语言(c、c++、ruby、python、HTML 等)。
Calling the weird multi-line handling a feature rather than a bug, just indicates a flawed language design. In the end it will always be a bug in the eyes of the user. Multi-line support is a basic feature of any main stream language and the lack of this feature is just plain annoing - just like the M$ paperclip, which I believe was also an attempt to guide the user.
将奇怪的多行处理称为功能而不是错误,只是表明语言设计有缺陷。最终在用户眼中永远是个bug。多行支持是任何主流语言的基本功能,缺少此功能只是很烦人 - 就像 M$ 回形针一样,我相信它也是一种引导用户的尝试。
That being said, HAML is a fantastically compact and useful language for writing HTML. Those of us who (in some cases) prefer multi-line blocks would just love to at least be offered some kind of configuration option to enable/disable decent multi line block support - regardless of the language designer's personal definition of "easy to read code".
话虽如此,HAML 是一种非常紧凑且有用的编写 HTML 的语言。我们这些(在某些情况下)更喜欢多行块的人会喜欢至少提供某种配置选项来启用/禁用体面的多行块支持 - 无论语言设计者对“易于阅读的代码”的个人定义如何”。
Until we get there, I guess we'll have to work around the language using the "-#" hack...
在我们到达那里之前,我想我们将不得不使用“-#”黑客来解决该语言......
回答by hgmnz
You could use a block on your helper, yielding whatever makes sense.
你可以在你的助手上使用一个块,产生任何有意义的东西。
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
Now on your 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'
回答by jdl
It's a hack (sort-of), but you could always use a "+" instead of a "=" on your 2nd, 3rd, etc. lines in the chain.
这是一个黑客(排序),但您始终可以在链中的第 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' |

