Class: Locomotive::Steam::Liquid::Tags::Paginate

Inherits:
Liquid::Block
  • Object
show all
Includes:
Concerns::Attributes
Defined in:
lib/locomotive/steam/liquid/tags/paginate.rb

Overview

Paginate a collection (array or from a DB).

Usage:

paginate contents.projects by 5 %

 {% for project in paginate.collection %}
   {{ project.name }}
 {% endfor %}
{% endpaginate %}

Constant Summary collapse

Syntax =
/(#{::Liquid::QuotedFragment}+)\s+by\s+(#{::Liquid::QuotedFragment}+)/o

Instance Attribute Summary collapse

Attributes included from Concerns::Attributes

#attributes, #raw_attributes

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ Paginate

Returns a new instance of Paginate.



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/locomotive/steam/liquid/tags/paginate.rb', line 24

def initialize(tag_name, markup, options)
  if markup =~ Syntax
    @collection_name  = $1
    @per_page         = $2

    parse_attributes(markup)
  else
    raise ::Liquid::SyntaxError.new('Valid syntax: paginate <collection> by <number>')
  end

  super
end

Instance Attribute Details

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



22
23
24
# File 'lib/locomotive/steam/liquid/tags/paginate.rb', line 22

def collection_name
  @collection_name
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



22
23
24
# File 'lib/locomotive/steam/liquid/tags/paginate.rb', line 22

def per_page
  @per_page
end

Instance Method Details

#render(context) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/locomotive/steam/liquid/tags/paginate.rb', line 37

def render(context)
  evaluate_attributes(context)

  context.stack do
    pagination = context['paginate'] = paginate_collection(context)

    path = sanitize_path(context['fullpath'])

    build_next_previous_links(pagination, path)

    if pagination['total_pages'] > 1
      build_parts(pagination, path)
    end

    super
  end
end