Mike Slinn

jekyll_nth

Published 2020-10-03. Last modified 2023-05-18.
Time to read: 1 minutes.

This page is part of the 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:

Shell
group :jekyll_plugins do 
  gem 'jekyll_nth'
end 

And then execute:

Shell
$ 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.

Shell
{{ "1,2,3,4,5" | split: ',' | nth: 2 }}  # returns 3


* indicates a required field.

Please select the following to receive Mike Slinn’s newsletter:

You can unsubscribe at any time by clicking the link in the footer of emails.

Mike Slinn uses Mailchimp as his marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp’s privacy practices.