多个多行HAML块 [英] Multiple multi-line HAML blocks

查看:69
本文介绍了多个多行HAML块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the (intentionally) strange multi-line format for HAML, I'd like to have the following lines in my template:

= 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' |

我发现的唯一可行的解​​决方案是在它们之间运行空白的Ruby代码行.看起来真的很丑.

The only working solution I have found is to run a blank line of Ruby code between. Which looks really ugly.

= 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' |

还有什么更好的吗?

推荐答案

这是一项功能,而不是错误. Haml多行块是故意笨拙的-包括一个接一个地难以遵循的-因为几乎总是将Ruby代码放入帮助程序中是更好的选择.即使仅调用一次助手,它也将使您的模板更易于阅读.例如:

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:

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中,做

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' |

这篇关于多个多行HAML块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆