Module: Facades::Helpers::Navigation

Defined in:
lib/facades/helpers/navigation.rb

Overview

Convenience helpers for building navigation lists, and defining ‘on’ states.

Instance Method Summary collapse

Instance Method Details

Creates a navigational list format, including a parent list / wrapper. Useful for nested list navigation

Examples:

Create a nested list navigation

nav do
  nav_link('About Page', '/about')
end
<ol>
  <li><a href="/home">Home Page</a>
    <ol>
        <li><a href="/about">Sub Page</a></li>
    </ol>
  </li>
</ol>

Parameters:

  • text (String)

    The text used within the link

  • path (String)

    The url used as the link’s href attribute

  • attrs (Hash)

    Hash of attributes to be applied to the link. This format is the same as Rail’s link_to helper

  • wrapper (Symbol)

    The html tag to be used as the wrapper. Defaults to :li

  • container (Symbol) (defaults to: :ol)

    The element that will be used as a container for the nested list

  • &block (Block)

    A block containing the content of the nested items



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/facades/helpers/navigation.rb', line 77

def nav(container = :ol, html_attrs = {}, heading = nil, &block)
  
  wrapper_attrs = html_attrs.delete(:wrapper) || {}
  
  built = if Facades.enable_html5
    inner = (container, capture(&block), wrapper_attrs)
    inner = ((:h3, heading) << inner) unless heading.nil?
    (:nav, html_attrs) do
      inner
    end          
  else
    (container, capture(&block), html_attrs)
  end
  built = built.html_safe if built.respond_to?(:html_safe)
  built
  
end

Creates a link wrapped in a list item, to be used within a list-based navigation.

Examples:

Create a list item link to any page

nav_link('Home Page', '/home') #=> <li><a href="/home">Home Page</a>

‘Active state’ functionality for the current page

nav_link('Current Page', '/current_url') #=> <li class="on"><a href="/current_url" class="on">Current Page</a></li>

Parameters:

  • text (String)

    The text used within the link

  • path (String)

    The url used as the link’s href attribute

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

    Hash of attributes to be applied to the link. This format is the same as Rail’s link_to helper

  • wrapper (Symbol) (defaults to: :li)

    The html tag to be used as the wrapper. Defaults to :li

Options Hash (attrs):

  • :active_class (String)

    The class to use if this link is ‘active’. Defaults to “on”

  • :proc (Proc)

    A callback which, when called, determines the active state of the link

  • :matcher (Regex)

    A regular expression to be matched against path



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/facades/helpers/navigation.rb', line 27

def nav_link(text, path, attrs = {}, wrapper = :li, &block)
  
  if block_given?
    return sub_nav_link(text, path, attrs, wrapper, :ol, &block)
  end
  
  link_attrs    = update_link_attrs(path, attrs)
  wrapper_attrs = link_attrs.delete(:wrapper)      
  child_link    = link_to(text, path, link_attrs)
  built = (wrapper === false ? child_link : (wrapper, child_link, wrapper_attrs))
  built = built.html_safe if built.respond_to?(:html_safe)
  built
end