I am trying to create a simple nested html menu using HAML and do not know how to do this by inserting elements with the right indentation , or the general best way to build nested trees. I would like to be able to do something similar, but infinitely deep:
- categories.each_key do |category| %li.cat-item{:id => "category-#{category}"} %a{:href => "/category/#{category}", :title => "#{category.titleize}"} = category.titleize
It seems to me that I could do this quite easily without resorting to manually writing tags in html, but I'm not the best with recursion. Here is the code I just came up with:
View Assistant
def menu_tag_builder(array, &block) return "" if array.nil? result = "<ul>\n" array.each do |node| result += "<li" attributes = {} if block_given? text = yield(attributes, node) else text = node["title"] end attributes.each { |k,v| result += " #{k.to_s}='#{v.to_s}'"} result += ">\n" result += text result += menu_tag_builder(node["children"], &block) result += "</li>\n" end result += "</ul>" result end def menu_tag(array, &block) haml_concat(menu_tag_builder(array, &block)) end
View
# index.haml, where config(:menu) converts the yaml below
YAML example defining a menu
menu: - title: "Home" path: "/home" - title: "About Us" path: "/about" children: - title: "Our Story" path: "/about/our-story"
Any ideas how to do this, the output is as follows:
<ul> <li class='one two'> Home </li> <li class='one two'> About Us </li> </ul>
... not this way:
<ul> <li class='one two'> Home</li> <li class='one two'> About Us</li> </ul>
... and therefore he is rightly taken away all over the world.
Thanks for the help, Spear