Class: Locomotive::Liquid::Tags::Nav

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/locomotive/liquid/tags/nav.rb

Overview

Display the children pages of the site, current page or the parent page. If not precised, nav is applied on the current page. The html output is based on the ul/li tags.

Passing through depth will control how many nested children are output

Usage:

nav site % => <ul class=“nav”><li class=“on”><a href=“/features”>Features</a></li></ul>

{% nav site, no_wrapper: true, exclude: ‘contact|about’, id: ‘main-nav’, class: ‘nav’, active_class: ‘on’ }

Constant Summary collapse

Syntax =
/(#{::Liquid::Expression}+)?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ Nav

Returns a new instance of Nav.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/locomotive/liquid/tags/nav.rb', line 19

def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    @source = ($1 || 'page').gsub(/"|'/, '')
    @options = { :id => 'nav', :depth => 1, :class => '', :active_class => 'on', :bootstrap => false }
    markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }

    @options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]

    @options[:add_attributes] = []
    if @options[:snippet]
      template = @options[:snippet].include?('{') ? @options[:snippet] : context[:site].snippets.where(:slug => @options[:snippet] ).try(:first).try(:template)
      unless template.blank?
        @options[:liquid_render] = ::Liquid::Template.parse(template)
        @options[:add_attributes] = ['editable_elements']
      end
    end

  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <page|site> <options>")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/locomotive/liquid/tags/nav.rb', line 43

def render(context)
  children_output = []

  entries = fetch_entries(context)

  entries.each_with_index do |p, index|
    css = []
    css << 'first' if index == 0
    css << 'last' if index == entries.size - 1

    children_output << render_entry_link(p, css.join(' '), 1)
  end

  output = children_output.join("\n")

  if @options[:no_wrapper] != 'true'
    list_class  = !@options[:class].blank? ? %( class="#{@options[:class]}") : ''
    output      = %{<nav id="#{@options[:id]}"#{list_class}><ul>\n#{output}</ul></nav>}
  end

  output
end