Hello. My name is Ian G… I am new to Radiant and new to ruby.
I am trying to figure out how I can get the menu for pages I create to
be autogenerated for me, like the breadcrumbs. I was thinking it would
involve the radius tags and maybe the creation of a behavior so that
if I create a page part say: sidebar then I could include a tag say
<r:menu /> then I could make it so that it automatically creates a
menu based on the pages from the root all the way down to the current
page.
Anyway, let me know if anyone thinks this is worthwhile and how I
might go about implementing it.
You may be able to modify the “sitemap” snippet that’s on the wiki to do
just that, perhaps with only one global tag creation. I’ve been mulling
around how to do this. I’ll get back to you. (I sense a new “how to”
article.)
Then our new global tag definition (I’m using the PageContext syntax):
define_tag ‘if_ancestor_or_self’ do |tag|
page = tag.globals.page
ancestors = tag.globals.page.ancestors
context_page = tag.locals.page
tag.expand if context_page == page or
ancestors.include?(context_page)
end
This should, starting from the root page, create a contextual menu
including
the siblings of all of the parent pages and the children of the current
page.
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 = “
<a
href="#{context_page.url}">#{context_page.title}\n”
if context_page.has_children? && (root_page == context_page ||
ancestors.include?(context_page))
result << “
\n”
context_page.children.each do |child|
result << recurse_menu(root_page, child, ancestors)
end
result << “
\n”
end
result << “
\n”
end
end
If anyone thinks it would be worthwhile, I can see what I can do about
packaging this up into a plugin.
Because of a limitation in the way that snippets are rendered, the code
I
posted earlier will not limit which children of the root of your sitemap
are
rendered. Essentially, everytime you get to the <r:if_ancestor_or_self>
tag, tag.globals.page will equal tag.locals.page. I intend to leave a
more
detailed ticket on the Trac.