Having migrated Justkez.com to be based on Jekyll, I was pondering how I might include my recent twitterings on the front page of the site. In the Wordpress world, this would have been done via a plugin which may or may not have hung the loading of the page, might have employed caching, but would certainly have had some overheads.

Not in Jekyll.

Integrating Jekyll and Twitter

It is rather simple to create a Ruby script to pull down your most recent Twitter updates and dump them into a file.

It is also simple to wrap each update in some rudimentary HTML.

We then use the Liquid include tag to insert the updates where desired.

The Ruby script

I have the following sat in my ~/bin/ directory, which can be executed by a cron job at whatever interval you see fit:

[code] require 'twitter' twitter_user = 'JohnDoe' # TODO: Change to your Twitter username puts '<ul id="twitter_list">' Twitter::Search.new.from(twitter_user).each do |r| d = DateTime.parse(r.created_at).strftime('%d %b') puts "<li><span class=\"gentle\">#{d}</span> #{r.text}

All this does is fetch the latest updates for twitter_users, and wrap each one in an HTML <li> tag.

Including the HTML

This one is pretty straightforward, as Liquid (Jekyll's templating engine) supports the inclusion of partials/fragments.

  • Create an _includes directory in your Jekyll directory (not in _site)

  • Add the liquid include line {&percent; include twitter.html &percent;} where the latest updates will go (for me this was in index.html)

  • Now you can populate _includes/twitter.html by running the above Ruby script and dumping the output to file, ala: ruby ~/bin/script.rb > _includes/twitter.html.

Finishing up

You will need to regenerate the site, and you would ideally run it in auto mode. Now, whenever the cron job updates the HTML file, Jekyll will regenerate the relevant files for you.

There you have it, seamless Twitter and Jekyll integration.