1. Code
  2. JavaScript
  3. jQuery

Building a Better Blogroll: Dynamic Fun with SimplePie and jQuery

Scroll to top
12 min read

A traditional blogroll is a simple list of other sites, often in the sidebar, that are related, owned by, or otherwise friendly to the home site. Blogrolls are a great idea and on-point with the spirit of blogging, but how is a casual reader to know if any of these sites are truly of interest? Let's improve upon the concept of a blogroll by not just listing sites, but dynamically pulling recent headlines from them, and using some fun jQuery animation.

Step 1 - The Idea and the Technologies

For our example, we will be using the TUTS family of sites. The idea here is to build a "widget" (for lack of a better term). Along the top of our widget will be a line of logos from each of the different sites in our blogroll. The TUTS family have lovely simple block logos which will make this easy, but the concept could be adapted to any logo. Each of the logos will act as "tabs" or buttons which when clicked, will display recent headlines from the respective site. Here is quick overview of the technologies we will use

  • Of course we will be using XHTML/CSS to build and style the widget.
  • We will use jQuery and the Coda Slider plugin for the fun animation.
  • The recent headlines will be dynamically pulled using PHP and SimplePie.

Because of the use of PHP, you won't be able to download the source code, open it up on your local machine, and view the demo. You'll need to either run PHP on your local machine, or upload it to your own server to play with it.

Step 2 - Designing

Design first, code later. I find the results are always better this way. A little playing around in Photoshop and we have a simple-yet-modern design to work with:

step 1step 1step 1

Note the use of guides. Being as clean and accurate with your design work is just as valuable as being clean and accurate with your code! Note that the PSD files will be included in this demo.

I don't want to go into a ton of detail about how all the design work was done and how all the images were sliced out. Instead lets take a look at each one and I'll quickly explain.

body background Image for the background texture. Repeats across the entire body element.
tab Image for the PSDTUTS tab. Bottom half is for "on" position, top half is for "off" position.
tab Image for the VECTORTUTS tab. Bottom half is for "on" position, top half is for "off" position.
tab Image for the NETTUTS tab. Bottom half is for "on" position, top half is for "off" position.
tab Image for the AUDIOTUTS tab. Bottom half is for "on" position, top half is for "off" position.
tab Image for the "body" of the widget. This can repeat vertically so it can grow as tall as needed.
tab Image for the bottom of the widget.
tab Image for the "next" button, for moving ahead to the next tab.
tab Image for the "previous" button, for moving back to the previous tab.

Step 2 - Downloading the Files

Normally we might start by coding the XHTML/CSS we need for the widget first. That is usually the best way to go so you can ensure clean and semantic markup. However, for this example, I believe it will be easiest for us to download all the components we need to make this thing work, and then code around that. This is because the jQuery plugin "Coda Slider" we are using has a specific HTML structure it likes.

You'll need a home directory for this project, so go ahead and create a new folder. Then create an index.php file, a style.css file, and we'll need folders for images, inc, and js. More organization! Then download the following things:

  • Download the latest jQuery to your js folder and include in your head section.
  • Download the Coda Slider plugin and include the plugin file, as well as the easing and easing compatibility JavaScript files in your head section.
  • Download SimplePie. Put the simplepie.inc file in your inc folder. You don't need to include anything on the page for this just yet, we'll get into this later.

When you are through, the top of your index.php file should be like this:

1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
5
<head>
6
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
7
8
	<title>A Better Blogroll</title>
9
10
	<link rel="stylesheet" type="text/css" href="style.css" />
11
12
	<script src='js/jquery-1.2.6.min.js' type='text/javascript'></script>
13
	<script src="js/jquery-easing.1.2.js" type='text/javascript'></script>
14
	<script src="js/jquery-easing-compatibility.1.2.js" type='text/javascript'></script>
15
	<script src="js/coda-slider.1.1.1.js" type='text/javascript'></script>
16
</head>

Step 3 - The Markup

If you pop open the code for the demo that comes with the Coda Slider, you'll see there is quite a few "wrapping" DIVs going on. The whole slider is wrapped in one then all of the panels are wrapped in one, then each panel is wrapped and there is even an inner wrapper inside each panel. Looks a bit like "DIVitis" (The plague of amateur coders using way more DIVs than necessary). Fret not, however. While I can't argue that there is a lot of DIVs going on here, each of them serves a purpose unique to the slider and is necessary for its form and function. In fact, our unique design complicates things even more and requires the use of a few more DIVs here and there.

Here is the markup for the rest of the page, sans the actual content for each of the panels:

1
2
<body>
3
	
4
	<div id="blogSliderWrap">
5
		<div id="blogSlider">
6
			<div class="innerWrap">
7
				<div class="panelContainer">
8
					
9
					<div class="panel" title="PSDTUTS">
10
						<div class="wrapper">
11
	
12
							<!-- content for panel -->
13
	
14
						</div>
15
					</div>
16
	
17
					<div class="panel" title="NETTUTS">
18
						<div class="wrapper">
19
						
20
							<!-- content for panel -->
21
						
22
						</div>
23
					</div>
24
				
25
					<div class="panel" title="VECTORTUTS">
26
						<div class="wrapper">
27
						
28
							<!-- content for panel -->
29
						
30
						</div>
31
					</div>
32
				
33
					<div class="panel" title="AUDIOTUTS">
34
						<div class="wrapper">
35
						
36
							<!-- content for panel -->
37
						
38
						</div>
39
					</div>
40
				</div>
41
			</div>
42
		</div>
43
		
44
		<div id="push"></div>
45
	</div>
46
47
</body>
48
49
</html>

Notice how there is no markup for the list of logos at the top? That markup gets dynamically generated by the plugin based on how many panel it detects. It also gives each of the list items a unique class name. We will use these to apply the images we created. Pretty fancy eh? This makes it very easy to add and remove panels from this slider in the future. If you want a peak at the code it creates, you can use something like Firebug to view the source after it has been rendered:

step 3step 3step 3

Step 4 - The CSS

Now that we have the markup squared away, we can set about to styling. Again, rather than go through every single selector here, I'll just show you the entire CSS file and then below I'll go over some of the important points. There is also some commenting within the CSS you may find of interest.

1
	
2
/*

3
	GENERIC RESET & BASIC STRUCTURE

4
*/
5
* 									{ margin: 0; padding: 0; }
6
body 								{ font-family: Verdana, Arial, Helvetica, sans-serif;
7
						  		 	  background: url(images/body-bg.jpg); font-size: 11px; }
8
a 									{ /* remove the dotted outlines when clicking tabs */ 
9
						  		      outline: none; text-decoration: none; }
10
ul 									{ list-style: none; }
11
12
13
/*

14
	WIDGET STRUCTURE

15
*/
16
#blogSliderWrap 					{ width: 320px; margin: 50px auto;
17
									  background: url(images/widget-bottom-bg.jpg) 7px bottom no-repeat;
18
									  padding-bottom: 50px; position: relative; }
19
#blogSlider 						{ margin-left: 15px; padding: 25px 18px 0px 18px; width: 263px;
20
									  background: url(images/widget-middle-bg.jpg) center center repeat-y; }
21
.stripViewer 						{ position: relative; overflow: hidden; clear: both; }
22
.stripViewer .panelContainer 		{ position: relative; left: 0px; top: 0px; }
23
.stripViewer .panelContainer .panel { float: left; position: relative; width: 280px; }
24
.wrapper 							{ padding: 10px; }
25
.innerWrap 							{ width: 273px; overflow: hidden; }
26
#push 								{ height: 35px; }
27
28
29
/* 

30
	MAIN TABBED NAVIGATION

31
*/
32
.stripNav 							{ margin-left: 22px; position: relative; top: 18px; z-index: 99;
33
									  width: 285px; }
34
.stripNav ul li 					{ float: left; }
35
36
.stripNav li a 						{ /* Generic "CSS image replacement" */
37
									  display: block; height: 18px; text-indent: -9999px; }
38
/* The below class names are auto-generated by the JavaScript */
39
.stripNav li.tab1 a { background: url(images/tab-psdtuts.jpg) top center no-repeat; width: 63px; }
40
.stripNav li.tab2 a { background: url(images/tab-nettuts.jpg) top center no-repeat; width: 63px; }
41
.stripNav li.tab3 a { background: url(images/tab-vectortuts.jpg) top center no-repeat; width: 85px; }
42
.stripNav li.tab4 a { background: url(images/tab-audiotuts.jpg) top center no-repeat; width: 73px; }
43
44
.stripNav li a:hover, 
45
.stripNav li a:active, 
46
.stripNav li a:focus 				{ /* Restore focus styling that we removed when we removed outlines */
47
									  background-position: top center; }
48
49
/* It is neccessary to repeat the image paths here */
50
.stripNav li.tab1 a.current { background: url(images/tab-psdtuts.jpg) bottom center no-repeat; }
51
.stripNav li.tab2 a.current { background: url(images/tab-nettuts.jpg) bottom center no-repeat; }
52
.stripNav li.tab3 a.current { background: url(images/tab-vectortuts.jpg) bottom center no-repeat; }
53
.stripNav li.tab4 a.current { background: url(images/tab-audiotuts.jpg) bottom center no-repeat; }
54
55
56
/* 

57
	SUB NAVIGATION

58
*/
59
.stripNavL a, .stripNavR a 			{ display: block; position: absolute; width: 75px; 
60
							 		  height: 22px; text-indent: -9999px; }
61
.stripNavL a						{ bottom: 35px; left: 45px; background: url(images/previous.png); }
62
.stripNavR a 						{ bottom: 35px; right: 45px; background: url(images/next.png);
63
}
64
65
66
/* 

67
	HEADLINES STYLING 

68
*/
69
.panel ul 							{ width: 240px; }
70
.panel ul li a 						{ border-bottom: 1px solid #C1B599; color: #303030;
71
						  		  	  display: block; padding: 7px 10px; }
72
.panel ul li:last-child a 			{ /* Remove bottom border on last list item

73
	   							  	  Hooray for pseudo selectors! */
74
								  	  border: none; }
75
								  	  /* Unique rollover colors for each list */
76
.panel ul#psd-list li a:hover 		{ background: #b20202; color: white; }
77
.panel ul#net-list li a:hover 		{ background: #4a8c7f; color: white; }
78
.panel ul#vector-list li a:hover 	{ background: #1f5293; color: white; }
79
.panel ul#audio-list li a:hover 	{ background: #4f7c11; color: white; }
  • The outer-most wrap (#blogSliderWrap) contains the background image for the bottom of the widget. Laying on top of that is the "expandable" part of widget, which contains the vertically-repeatable background image (#blogSlider)
  • In order to keep the content from appearing to "fly in" from outside of the main white area, yet another wrap was needed (.innerWrap) that slims the inside down even further to prevent that. This is unique to our super-cool drop shadowed design.
  • As I mentioned earlier, the markup for the main navigation is generated on-the-fly by the JavaScript, but that doesn't mean we can't still style it with CSS. The style for (.stripNav) uses relative positioning to kick it over into place as well as ensure that it is on top of all over page elements.
  • We use the unique class names the JavaScript gives us to use basic CSS image replacement and apply our images to each tab. Note that shared attributes (like height) are applied generically while unique attributes (like width) are applied specifically.
  • The JavaScript also auto-generates two special page elements for moving ahead a single tab or moving backward a single tab. We apply images and positioning to these as well
  • Ultimately, each panel will contain and unordered list of headlines. We will give each of these a unique ID. For a bonus nice touch, each of them will have a rollover color that matches the logo color of the originating site.

Step 5 - Activating the Slider

Now with the images, markup, and CSS all in place, we can kick off the slider. Include the following JavaScript at the bottom of your head section.

1
2
<script type='text/javascript'>
3
	$(function () {
4
		$("#blogSlider").codaSlider();
5
	});
6
</script>

Now we have a perfectly functional slider, with nothing in it.

step 3step 3step 3

Step 6 - Pulling the Headlines with SimplePie

SimplePie is a PHP feed parser which makes it pretty darn easy to pull in RSS feeds and use the content as we wish. In our example, all we need are the headlines and links, but pulling the feed is always the same regardless of what you use.

At the very top of your index.php file, include the following PHP code:

1
2
<?php
3
	require_once('inc/simplepie.inc');
4
	$total_articles = 10;
5
6
	$psdtutsfeed = new SimplePie('http://feeds.feedburner.com/psdtuts');
7
	$psdtutsfeed->handle_content_type();
8
	for ($x = 0; $x < $psdtutsfeed->get_item_quantity($total_articles); $x  )
9
	{
10
		$psdtutsarticles[] = $psdtutsfeed->get_item($x);
11
	}
12
?>

This code will go out and get the RSS feed from the main PSDTUTS RSS feed (http://feeds.feedburner.com/psdtuts). Then we create an array of the 10 most recent headlines. Now down in our "panel" for PSDTUTS, we'll need to cycle through this array and echo out the linked headlines. Here is the new code for the PSDTUTS panel:

1
2
<div class="panel" title="PSDTUTS">
3
	<div class="wrapper">
4
5
		<ul id="psd-list">
6
			<?php

7
				foreach ($psdtutsarticles as $item)

8
				{

9
					echo '<li><a href="' . $item->get_link() . '">' . $item->get_title() . '</a></li>';

10
				}

11
			?>
12
		</ul>
13
14
	</div>
15
</div>

You can guess where we need to go from here... We need to do the exact same thing for each of our four panels. No need to list all the code out here, as all we do is create new variables for each of the feeds, pull them in, and create new arrays for each of those feeds. Then we loop through each of those new arrays just like we did for PSDTUTS to echo out the linked headlines.

Notice how the unordered list we used for the PSDTUTS headlines had a unique ID of "psd-list". That isn't entirely necessary, but I have used that as a hook in this example to apply a different rollover color for each headline list.

Our widget is now nearly complete!

step 6step 6step 6

Step 7 - Fixing up IE

Despite being fully valid code, both IE6 and IE7 were showing some quirks with the background images. Oh well, our super-cool drop shadow isn't in any way necessary. We can leave it in place as "progressive enhancement" for folks with quality modern browsers and pull it out for IE, leaving just a simple white box.

Include an IE-Only Stylesheet in your head section like so:

1
2
<!--[if IE]>

3
	<link rel="stylesheet" type="text/css" href="style-ie.css" />

4
<![endif]-->

Then create the style-ie.css file in your home directory and include the following:

1
2
#blogSliderWrap {
3
	background: white;
4
	width: 285px;
5
	position: relative;
6
	overflow: hidden;
7
}
8
9
#blogSlider {
10
	background: white;
11
	width: 285px;
12
	padding: 0;
13
	margin: 0;
14
}
15
16
.stripNav {
17
	margin: 0;
18
	top: 0px;
19
}

Now everything is A-OK in Internet Explorer. The background is simply just white:

step 6step 6step 6

So there you have it, a better blogroll for all! Clearly, the technologies presented here can be used for all kinds of interesting things beyond a simple blogroll. The slider is a fantastic plugin that could used for a photo gallery, a portfolio, step-by-step instructions, or any number of applications. The ability to pull in RSS feeds to use on web pages is also very powerful (and potentially abuse-able). See the SimplePie documentation for all you can do with it, and remember to use it for the powers of good!

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.