In building hierarchical HTML structures, it's most elegant to use
recursive blocks of haml. Here is a demo recursive haml script:
%html
%body
- p = Proc.new do |data, block|
%ul
- data.each do |l|
%li
- if l.class == Array
- block.call(l, block)
- else
= l
- p.call([[1,2,3],[4,5,6],[7,8,9]], p)
This works, except for one little quibble: the HTML output isn't
indented properly. It renders just fine. What I'd like to know is:
How do I fix the indentation?
Is this the most elegant way to write recursive blocks in haml?
Thanks
Bruce
- Nathan
def nice_bit
Proc.new do |data, block|
open(:ul) do
data.each do |item|
open(:li, (item.class == Array ? block.call(item, block) : item))
end
end
end
p.call([[1,2,3],[4,5,6],[7,8,9]], p)
end
Something like that?
(untested code!)
-hampton.