[ACCEPTED]-Multiple multi-line HAML blocks-haml
This is a feature, not a bug. Haml multiline 10 blocks are intentionally unwieldy - including 9 hard to follow one after another - because 8 almost all the time it's better to put that 7 Ruby code into a helper. Even if the helper 6 is only called once, it will make your template 5 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
Then in 4 your Haml, just do
= blatz_link
= blootz_link
which will be much more 3 readable and easier to understand.
If you 2 absolutely must follow one multiline block 1 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' |
I've encountered the same problems and workarounds 30 as have been mentioned here, and the weird 29 (and yes, it IS weird) behavior of HAML 28 with regard to multi-line blocks has bitten 27 me quite a few times. I know that it's intentional 26 and that it was probably intended to force 25 the user to make his code easier to read. However, it's 24 a well known fact that every developer has 23 his own preferences when it comes to structuring 22 code. HAML is the only language I know of 21 (c, c++, ruby, python, HTML, etc.) that 20 tries to impose such restrictions.
Calling 19 the weird multi-line handling a feature 18 rather than a bug, just indicates a flawed 17 language design. In the end it will always 16 be a bug in the eyes of the user. Multi-line 15 support is a basic feature of any main stream 14 language and the lack of this feature is 13 just plain annoing - just like the M$ paperclip, which 12 I believe was also an attempt to guide the 11 user.
That being said, HAML is a fantastically 10 compact and useful language for writing 9 HTML. Those of us who (in some cases) prefer 8 multi-line blocks would just love to at 7 least be offered some kind of configuration 6 option to enable/disable decent multi line 5 block support - regardless of the language 4 designer's personal definition of "easy 3 to read code".
Until we get there, I guess 2 we'll have to work around the language using 1 the "-#" hack...
You could use a block on your helper, yielding 1 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'
It's a hack (sort-of), but you could always 2 use a "+" instead of a "=" on your 2nd, 3rd, etc. lines 1 in the chain.
= 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' |
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.