Module: Ramaze::Helper::MenuFrontend

Defined in:
lib/zen/package/menus/lib/menus/helper/menu_frontend.rb

Overview

Helper that allows you to render navigation menus in your templates.

Since:

Instance Method Summary (collapse)

Instance Method Details

- (String) render_menu(menu, options = {})

Renders a navigation menu's items based on the menu slug or ID.

Examples:

Render a menu by it's slug

render_menu('main')

Render a menu by it's ID

render_menu(2)

Render a menu without sub items

render_menu('main', :sub => false)

Parameters:

  • menu (String|Fixnum)

    The slug or ID of a menu to render.

  • options (Hash) (defaults to: {})

    A hash containing various options to customize the return value.

Options Hash (options):

  • :limit (Fixnum)

    The amount of menu items to display. Set to 20 by default.

  • :sub (TrueClass|FalseClass)

    Boolean that indicates if sub items should be rendered or not, set to true by default.

  • :order (Symbol)

    The sort order method to call, can either be :asc or :desc, set to :asc by default.

Returns:

  • (String)

Since:

  • 0.3



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zen/package/menus/lib/menus/helper/menu_frontend.rb', line 35

def render_menu(menu, options = {})
  options = {
    :limit => 20,
    :sub   => true,
    :order => :asc
  }.merge(options)

  menu  = Menus::Model::Menu.find_by_pk_or_slug(menu)
  items = Menus::Model::MenuItem \
    .filter(:parent_id => nil, :menu_id => menu.id) \
    .limit(options[:limit]) \
    .order(:sort_order.send(options[:order])) \
    .eager(:parent) \
    .all

  g     = Ramaze::Gestalt.new
  attrs = {}
  tree  = {}

  if !menu.html_class.nil? and !menu.html_class.empty?
    attrs[:class] = menu.html_class
  end

  if !menu.html_id.nil? and !menu.html_id.empty?
    attrs[:id] = menu.html_id
  end

  # Build the HTML
  g.ul(attrs) do
    items.each do |item|
      if item.parent_id.nil?
        render_menu_item(item, g, options)
      end
    end
  end

  return g.to_s
end

- (Object) render_menu_item(item, g, options = {}) (private)

Generates the HTML for a single menu item.

Parameters:

  • item (Menus::Model::MenuItem)

    The menu item to render.

  • g (Ramaze::Gestalt)

    An instance of Ramaze::Gestalt to use for building the HTML.

  • options (Hash) (defaults to: {})

    A hash of options, see the options hash for #render_menu for more information.

Since:

  • 0.3



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/zen/package/menus/lib/menus/helper/menu_frontend.rb', line 86

def render_menu_item(item, g, options = {})
  attrs = {}

  if !item.html_class.nil? and !item.html_class.empty?
    attrs[:class] = item.html_class
  end

  if !item.html_id.nil? and !item.html_id.empty?
    attrs[:id] = item.html_id
  end

  g.li(attrs) do
    g.a(:href => item.url, :title => item.name) { item.name }

    # Render any sub menu items
    if options[:sub] == true
      children = item.children

      if !children.empty?
        g.ul(:class => :children) do
          children.each { |child| render_menu_item(child, g, options) }
        end
      end
    end
  end
end