Class: Jekyll::CategoryIndexPage

Inherits:
Page
  • Object
show all
Defined in:
lib/jekyll/category_pages.rb

Overview

Auto-generated page for a category index.

When pagination is enabled, contains a CategoryPager object as paginator. The posts in the category are always available as posts, the total number of those is always total_posts.

Constant Summary collapse

ATTRIBUTES_FOR_LIQUID =

Attributes for Liquid templates.

%w(
  category
  paginator
  posts
  total_posts
  content
  dir
  name
  path
  url
)

Instance Method Summary collapse

Constructor Details

#initialize(site, dir, page_name, category, category_layout, posts_in_category, use_paginator) ⇒ CategoryIndexPage

Initialize a new category index page.

site - The Site object. dir - Base directory for all category pages. page_name - Name of this category page (either ‘index.html’ or ‘page#.html’). category - Current category as a String. category_layout - Name of the category index page layout (must reside in the ‘_layouts’ directory). posts_in_category - Array with full list of Posts in the current category. use_paginator - Whether a CategoryPager object shall be instantiated as ‘paginator’.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/jekyll/category_pages.rb', line 154

def initialize(site, dir, page_name, category, category_layout, posts_in_category, use_paginator)
  @site = site
  @base = site.source
  if ! File.exist?(File.join(@base, category_layout)) && 
    ( site.theme && File.exist?(File.join(site.theme.root, category_layout)) )
      @base = site.theme.root
  end
  
  super(@site, @base, '', category_layout)
  @dir = dir
  @name = page_name

  self.process @name

  @category = category
  @posts_in_category = posts_in_category
  @my_paginator = nil

  self.read_yaml(@base, category_layout)
  self.data.merge!('title' => category)
  if use_paginator
    @my_paginator = CategoryPager.new
    self.data.merge!('paginator' => @my_paginator)
  end
end

Instance Method Details

#add_paginator_relations(page, per_page, total_pages, previous_page_path, next_page_path, previous_page, next_page) ⇒ Object

Add relations of this page to other pages handled by a CategoryPager.

Note that this method SHALL NOT be called if the category pages are instantiated without pagination. This method SHALL be called if the category pages are instantiated with pagination.

page - Current page number. per_page - Posts per page. total_pages - Total number of pages. previous_page - Number of previous page or nil. next_page - Number of next page or nil. previous_page_path - String with path to previous page or nil. next_page_path - String with path to next page or nil.



192
193
194
195
196
197
198
199
200
# File 'lib/jekyll/category_pages.rb', line 192

def add_paginator_relations(page, per_page, total_pages, previous_page_path, next_page_path, previous_page, next_page)
  if @my_paginator
    @my_paginator.add_relations(page, per_page, total_pages,
                                previous_page, next_page, previous_page_path, next_page_path)
    @my_paginator.add_posts(page, per_page, @posts_in_category)
  else
    Jekyll.logger.warn("Categories", "add_relations does nothing since the category page has been initialized without pagination")
  end
end

#categoryObject

Get the category name this index page refers to

Returns a string.



205
206
207
# File 'lib/jekyll/category_pages.rb', line 205

def category
  @category
end

#paginatorObject

Get the paginator object describing the current index page.

Returns a CategoryPager object or nil.



212
213
214
# File 'lib/jekyll/category_pages.rb', line 212

def paginator
  @my_paginator
end

#postsObject

Get all Posts in this category.

Returns an Array of Posts.



219
220
221
# File 'lib/jekyll/category_pages.rb', line 219

def posts
  @posts_in_category
end

#total_postsObject

Get the number of posts in this category.

Returns an Integer number of posts.



226
227
228
# File 'lib/jekyll/category_pages.rb', line 226

def total_posts
  @posts_in_category.size
end