Module: Jekyll::Relations::Page

Defined in:
lib/jekyll/relations/page.rb

Instance Method Summary collapse

Instance Method Details

#ancestorsObject Also known as: parents

A list of ancestors from the site index to the current page.

Returns an Array of Page objects or an empty Array.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jekyll/relations/page.rb', line 22

def ancestors
  @ancestors ||= begin
    return [] unless parent
    ancestors = []
    i = self
    while i.parent
      ancestors << i.parent
      i = i.parent
    end
    ancestors.reverse
  end
end

#childrenObject

A list of the pages directly below this one in the folder structure.

Returns an Array of Page objects or an empty Array.



39
40
41
42
43
44
45
46
47
# File 'lib/jekyll/relations/page.rb', line 39

def children
  return [] unless html? && index?
  @children ||= begin
    depth = url.count('/')
    site.pages
      .select(&:html?)
      .select { |p| p.url.start_with?(url) && p.url.count('/') == depth + 1 }
  end
end

#parentObject

The next highest page in the folder structure.

Returns the Page object for the parent page.



9
10
11
12
13
14
15
16
17
# File 'lib/jekyll/relations/page.rb', line 9

def parent
  @parent ||= begin
    return nil unless html?
    depth = url.count('/')
    site.pages
      .select(&:html?)
      .find { |p| url.start_with?(p.url) && p.url.count('/') == depth - 1 }
  end
end

#to_liquid(attrs = Jekyll::Page::ATTRIBUTES_FOR_LIQUID) ⇒ Object

Convert the new Page data to a Hash suitable for use by Liquid.

Returns the Hash representation of this Page.



52
53
54
# File 'lib/jekyll/relations/page.rb', line 52

def to_liquid(attrs = Jekyll::Page::ATTRIBUTES_FOR_LIQUID)
  super(attrs + %w(parent ancestors parents children))
end