How to easily use Airtable Data in Jekyll


#1

It’s super easy to use Airtable data in Jekyll. Here are the steps:

Step 1: Include the Airtable Gem in your gemfile.

gem 'airtable'

You may also need:

gem 'bigdecimal'
gem 'activesupport'
gem "json"

Step 2: Add a script to your _plugins folder

Make sure it has a .rb extension. The script should look something like this:

require 'json'
require 'airtable'
require 'active_support/all'
# require 'active_support/all'

# Pass in api key to client
@client = Airtable::Client.new("YOUR_API_KEY")

# Pass in the app key and table name

@table = @client.table("YOUR_APP_ID", "YOUR_TABLE_NAME")
@records = @table.records(:filterByFormula => "published", :sort => ["date", :desc], :limit => 100)

# Change the filename here below but make sure it's in the _data folder.
File.open("_data/YOUR_FILENAME.json", "w") do |f|
    data = @records.map { |record| record.attributes }
    f.write(data.to_json)
end

Step 3: Use that data in your themes.

Jekyll will run that script as part of it’s build process and make that data available to your themes. All you have to do it something like this:

{% for item in site.data.airtable %}

<p>{{item.name}}</p>
<p>{{item.description}}</p>
{% if  item.attachment %}
<img src="{{item.attachment[0].url}}" alt={{item.title}} />
{% endif %}

{% endfor %}

Check out rowandtable.com for some examples.