Class: Kin::Nav::Formatters::Basic

Inherits:
Object
  • Object
show all
Includes:
Merb::Helpers::Tag
Defined in:
lib/kin/nav/formatters.rb

Overview

Receives a nav instance and transforms it into HTML.

Direct Known Subclasses

HasRight, Subnav

Instance Method Summary collapse

Constructor Details

#initialize(nav, controller, options) ⇒ Basic

Creates a new BasicFormatter instance.

Parameters:

  • nav (Kin::Nav::Menu)

    An instance of a Menu to be rendered as HTML.

  • controller (#controller_name, #action_name)

    An object which behaves like a controller.

  • options (Hash)

    A hash of options for customising the menu. See Kin::Nav::Helper#display_navigation.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kin/nav/formatters.rb', line 25

def initialize(nav, controller, options)
  @nav = nav
  @current  = nav.active_item(controller)

  @resource = options[:resource]
  @inject   = options.fetch(:inject, {})
  @guards   = options.fetch(:guard, {})

  # Escape injected content.
  @inject.each do |item_id, contents|
    contents = Array(contents)
    contents.map! { |v| Merb::Parse.escape_xml(v) }
    @inject[item_id] = contents
  end
end

Instance Method Details

#to_htmlString

Transforms the menu given to initialize into a string containing HTML.

Returns:

  • (String)

    HTML containing the rendered menu. A collection of <li> elements contained in a <ul>.



51
52
53
54
55
# File 'lib/kin/nav/formatters.rb', line 51

def to_html
  '<ul>%s</ul>' % @nav.items.map do |item|
    trasform_item_to_html(item) if item.display?(@guards)
  end.join("\n")
end