Loading a blog feed on your website
Recently I needed to load recent blog posts onto a website. I discovered the Google Feed API which, as most things Google, is awesome. I also discovered this jQuery plugin which uses it along with what appears to be an old version of their Dynamic Feed Control. This plugin, like all of Mike Alsup’s other plugins, is great. However, I couldn’t get it to work exactly the way I wanted. So I built my own.
I also borrowed and modified some code from John Resig to format the dates. Oh yeah, it also depends on the jQuery Templating Plugin.
It takes an options argument with a required url property and a couple of other optional properties, gets the feed, and loads the items into semantic markup inside the selected element. You can then style it any way you want.
/**
* Plugin which uses the Google AJAX Feed API for creating feed content
* depends on jquery template plugin
* very loosely based on http://jquery.malsup.com/gfeed/
*/
(function ($) {
if (!$.template) {
alert('You must include the jQuery Templating Plugin script');
return;
}
//feed item template
var templateHtml = '<div>' +
'<div class="feed-item">' +
'<a href="${link}" target="_blank">' +
'<h2 class="feed-title">${title}</h2>' +
'</a>' +
'<div class="feed-snippet">${contentSnippet}</div>' +
'<div class="feed-footer">Posted ${publishedDate} by ${author}</div>' +
'</div>' +
'</div>';
//from based on code from John Resig
// - http://ejohn.org/blog/javascript-pretty-date/
function prettyDate(dateString) {
var date = new Date(dateString);
diff = (((new Date()).getTime() - date.getTime()) / 1000),
day_diff = Math.floor(diff / 86400);
if (isNaN(day_diff) || day_diff < 0) {
return;
}
return day_diff == 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor(diff / 60) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor(diff / 3600) + " hours ago") ||
day_diff == 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 42 && Math.ceil(day_diff / 7) + " weeks ago" ||
day_diff >= 42 && Math.floor(day_diff / 31) + " months ago";
}
new Date().tou
$.fn.feed = function (options) {
var opts = $.extend({
url: '',
target: this,
title: 'The latest from our blog',
max: 5 // max number of items
}, options || {});
//show a loading indicator
opts.target.append('<div id="feed-loading">loading blog feed...</div>');
//get the feed items
var feedItemTemplate = $(templateHtml).template();
var gFeedsUrl = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&num=' + opts.max + '&q='
$.ajax({
url: gFeedsUrl + encodeURIComponent(opts.url),
dataType: 'json',
success: function (response) {
//remove the loading indicator
$('#feed-loading').remove();
if (response && response.responseData
&& response.responseData.feed
&& response.responseData.feed.entries) {
var feedItems = response.responseData.feed.entries;
//map them to format the date
$.map(feedItems, function (item, idx) {
item.publishedDate = prettyDate(item.publishedDate);
return item;
});
//load the feeds into the dom
opts.target.append('<h1>' + opts.title + '</h1><div id="rss-feed"></div>')
.find('#rss-feed')
.append($.tmpl(feedItemTemplate, feedItems));
} else {
opts.target.append('<div id="feed-error">Unable to retrieve blog feed.</div>');
}
}
});
return this;
};
})(jQuery);