Module: Jekyll::Filters::Arrays

Defined in:
lib/jekyll/filters/arrays.rb

Instance Method Summary collapse

Instance Method Details

#concat_or_append(input, concatenable) ⇒ Object

Join arrays or append item to array

Examples:

{% assign things = site.posts | where: "layout", "thing" %}
{% assign being = site.posts | find: "layout", "being" %}
{% assign dread = things | concat_or_append: being %}

Parameters:

  • input (Array)
  • concatenable (Array, any)


29
30
31
32
33
34
35
36
37
# File 'lib/jekyll/filters/arrays.rb', line 29

def concat_or_append(input, concatenable)
  input = [] unless array? input

  if concatenable.is_a? Array
    input + concatenable
  else
    input.dup << concatenable
  end
end

#infinite_next(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>

Finds the next posts in a collection, starting from current post and restarts from the beginning to always return posts.

Examples:

{{ page | infinite_next: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)

    The current post (page)

  • posts (Array)

    An array of posts (ie. results from where filter)

  • amount (Integer) (defaults to: 1)

    Amount of posts next to current, max is posts size

Returns:

  • (Array<Jekyll::Document,Drop>)

    Array of posts



48
49
50
51
52
53
54
55
# File 'lib/jekyll/filters/arrays.rb', line 48

def infinite_next(input, posts, amount = 1)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts.rotate(index).slice(1, amount) if index
end

#infinite_prev(input, posts, amount = 1) ⇒ Array<Jekyll::Document,Drop>

Finds the previous posts in a collection, starting from current post and restarts from the beginning to always return posts.

Examples:

{{ page | infinite_next: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)

    The current post (page)

  • posts (Array<Jekyll::Document,Drop>)

    An array of posts (ie. results from where filter)

  • amount (Integer) (defaults to: 1)

    Amount of posts previous to current, max is posts size

Returns:

  • (Array<Jekyll::Document,Drop>)

    Array of posts



66
67
68
69
70
71
72
73
# File 'lib/jekyll/filters/arrays.rb', line 66

def infinite_prev(input, posts, amount = 1)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts.rotate(index).reverse.slice(0, amount).reverse if index
end

#next(input, posts) ⇒ Drop?

Return next post from an array or nothing if post is last.

Examples:

{{ page | next: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)
  • posts (Array<Jekyll::Document,Drop>)

Returns:

  • (Drop, nil)


82
83
84
85
86
87
88
89
# File 'lib/jekyll/filters/arrays.rb', line 82

def next(input, posts)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts[index + 1] if index
end

#prev(input, posts) ⇒ Drop? Also known as: previous

Return previous post from an array, or nothing if post is the first item.

Examples:

{{ page | prev: site.posts }}
{{ page | previous: site.posts }}

Parameters:

  • input (Jekyll::Document, Drop)
  • array (Array)

Returns:

  • (Drop, nil)


100
101
102
103
104
105
106
107
# File 'lib/jekyll/filters/arrays.rb', line 100

def prev(input, posts)
  posts = [] unless array? posts

  liquid_input = input.to_liquid
  liquid_posts = posts.map(&:to_liquid)
  index = find_in_stack(liquid_input, liquid_posts)
  liquid_posts[index - 1] if index&.positive?
end

#sample(input, amount = 1) ⇒ Any

Returns one or several random items from an Array.

Examples:

{{ site.posts | sample }}
{{ site.posts | sample: 3 }}

Parameters:

  • input (Array)
  • amount (Integer) (defaults to: 1)

Returns:

  • (Any)


14
15
16
17
18
# File 'lib/jekyll/filters/arrays.rb', line 14

def sample(input, amount = 1)
  input = [] unless array? input

  input.sample(amount)
end