Class: Noumenon::Template::CoreTags::NavigationTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/noumenon/template/core_tags.rb

Overview

Renders a navigation menu.

Examples:

<header>
  {% nav_menu %}
</header>

<div id="sidebar">
  <h1>About Our Stuff</h1>
  <nav>
    {% nav_menu root=/about depth=1 include_root=false %}
  </nav>
</div>

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, syntax, tokens) ⇒ NavigationTag

Returns a new instance of NavigationTag.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/noumenon/template/core_tags.rb', line 71

def initialize(tag_name, syntax, tokens)
  super
  
  @options = { depth: 1, root: "/", include_root: true }
 
  syntax.split(" ").each do |option|
    key, value = option.split("=")
    case key
    when "depth"
      @options[:depth] = value.to_i
    when "root"
      @options[:root] = value
    when "include_root"
      @options[:include_root] = [ "true", "yes", "1" ].include?(value)
    end
  end
end

Instance Method Details

#render(context) ⇒ Object



102
103
104
# File 'lib/noumenon/template/core_tags.rb', line 102

def render(context)
  render_list Noumenon.content_repository.children(@options[:root], @options[:depth], @options[:include_root])
end

#render_list(items) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/noumenon/template/core_tags.rb', line 89

def render_list(items)
  list = items.collect do |item|
    str  = "<li>"
    str << %Q{<a href="#{item[:path]}">#{item[:nav_title] || item[:title]}</a>}
    str << render_list(item[:children]) if item[:children]
    str << "</li>"
    
    str
  end
  
  "<ul>#{list.join("\n")}</ul>"
end