Published 2020-10-03.
Last modified 2023-05-18.
Time to read: 1 minutes.
jekyll_plugins
collection.
nth
is a Liquid filter that returns item n
of array, origin 0.
Installation
Add the following highlighted line to your Jekyll project's Gemfile
,
within the jekyll_plugins
group:
group :jekyll_plugins do gem 'jekyll_nth' end
And then execute:
$ bundle
Usage Example
Important: the name of this filter must be followed by a colon (:). If you fail to do that an error will be generated and the Jekyll site building process will halt. The error message looks something like this:
Liquid Warning: Liquid syntax error (line xxx):
Expected end_of_string but found string in
"{{ lines | nth 2 }}" in /some_directory/some_files.html Liquid Exception: Liquid error (line 285): wrong number of arguments (given 1, expected 2) in /some_directory/some_file.html Error: Liquid error (line 285):
wrong number of arguments (given 1, expected 2).
This example obtains the second item of the array.
Note that because
Liquid does not provide a syntax for defining arrays,
the following transforms a string of numbers to an array of numbers using split
.
{{ "1,2,3,4,5" | split: ',' | nth: 2 }} # returns 3