Working with submenu in RefineryCMS

Working with menu and sub-menu is quite easy in refineryCMS but I just cramped while displaying the submenus. Below are the code snippets i found to solve this issue

Call the partial file in your header

=render(:partial => “/refinery/menu”, :locals => { :dom_id => ‘menu’, :css => ‘menu’})

Write the following in your partial menu file

<%
# Collect the root items.
# ::Refinery::Menu is smart enough to remember all of the items in the original collection.
if (roots = local_assigns[:roots] || (collection ||= refinery_menu_pages).roots).present?
dom_id ||= ‘menu’
css = [(css || ‘menu clearfix’)].flatten.join(‘ ‘)
hide_children = Refinery::Core.menu_hide_children if hide_children.nil?
-%>
<ul>
<%= render :partial => ‘/refinery/menu_branch’, :collection => roots,
:locals => {
:hide_children => hide_children,
:sibling_count => (roots.length – 1),
:menu_levels => local_assigns[:menu_levels],
:apply_css => true #if you don’t care about class=’first’ class=’last’ or class=’selected’ set apply_css to false for speed.
} -%>
</ul>
<% end -%>

Write the following in your partial menu_branch file

<%
# Collect the root items.
# ::Refinery::Menu is smart enough to remember all of the items in the original collection.
if (roots = local_assigns[:roots] || (collection ||= refinery_menu_pages).roots).present?
dom_id ||= ‘menu’
css = [(css || ‘menu clearfix’)].flatten.join(‘ ‘)
hide_children = Refinery::Core.menu_hide_children if hide_children.nil?
is_selected_branch = ( selected_page_or_descendant_page_selected?(local_assigns[:menu_branch]) ? true : false)
-%>

<%roots.each_with_index do |page, index| %>
<% if page.children.present? && (children = page.children unless hide_children).present? && is_selected_branch && (!local_assigns[:menu_levels] || page.ancestors.length < local_assigns[:menu_levels])%>
<% page.children.each do |child| %>
<ul class=”firstLevelMenu”>
<li>
<%= link_to child.title, refinery.url_for(child.url)%>
</li>
</ul>
<%end%>
<% page.children.each do |child| %>
<%if child.children.present? %>
<ul class=”secondLevelMenu” style=”display: block”>
<% child.children.each_with_index do |grandchild,i|%>
<li>
<%= link_to grandchild.title, refinery.url_for(grandchild.url)%>
</li>
<%end%>
</ul>
<%end%>
<%end%>
<%end%>
<%end%>

<%end%>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s