[Radiant] Menu and Page Menus

Kevin Ansfield kev at digitalblueprint.co.uk
Thu Sep 21 12:48:10 CDT 2006


Hi Guys,

Unfortunately there appears to be some core behaviour stopping Seans 
code above from working - see trac ticket 
http://dev.radiantcms.org/radiant/ticket/450

I've put my own stopgap solution together, utilizing the logic that Sean 
posted:

----
class Behavior::Base
  class TagError < StandardError; end

  define_tags do

    tag 'menu' do |tag|
      url = tag.attr['url']
      if found = Page.find_by_url(tag.attr['url'])
        menu_root = found
      else
        menu_root = Page.find_by_url("/")
      end

      root_page = tag.locals.page
      ancestors = root_page.ancestors

      result = "<ul>\n"
      menu_root.children.each do |child|
        context_page = child
        result << recurse_menu(root_page, context_page, ancestors)
      end
      result << "</ul>\n"
    end

  end

  def recurse_menu(root_page, context_page, ancestors)
    result = "<li><a 
href=\"#{context_page.url}\">#{context_page.title}</a>\n"
    if context_page.has_children? && (root_page == context_page || 
ancestors.include?(context_page))
      result << "<ul>\n"
      context_page.children.each do |child|
        result << recurse_menu(root_page, child, ancestors)
      end
      result << "</ul>\n"
    end
    result << "</li>\n"
  end
end
----

If anyone thinks it would be worthwhile, I can see what I can do about 
packaging this up into a plugin.

Kevin Ansfield
http://www.digitalblueprint.co.uk

-- 
Posted via http://www.ruby-forum.com/.



More information about the Radiant mailing list