CSSKarma

display your <style>

designing the web since 2002

Posts Tagged ‘rss’

|

Selectable RSS with SimplePie and jQuery

Monday, August 17th, 2009

banner

I was building an RSS reader for a project last week and it turned out to be a cool little app. So I thought I’d share some of the code.

View demo

Download demo files

Let’s start off with the HTML we need to get some user interaction with the form. It’s a pretty basic, just some of checkboxes.

form with 4 checkboxes

HTML
<form action="" method="post" id="selection">
  <h2>Choose your news sources</h2>
 <!--
  Setting all the checkbox names to "source[] helps us build the array on submit"
  We're also adding the RSS feed url to the checkbox value so we can save it in the array values
  -->
  <ul>
  <li><input type="checkbox" id="csskarma" name="source[]" value="http://www.csskarma.com/blog/feed/"/><label for="csskarma">CSSKarma</label></li>
  <li><input type="checkbox" id="cnn" name="source[]" value="http://rss.cnn.com/rss/cnn_topstories.rss"/><label for="cnn">CNN</label></li>
  <li><input type="checkbox" id="espn" name="source[]" value="http://sports-ak.espn.go.com/espn/rss/news"/><label for="espn">ESPN</label></li>
  <li><input type="checkbox" id="vitamin" name="source[]" value="http://feeds.feedburner.com/vitaminmasterfeed"/><label for="vitamin">Think Vitamin</label></li>
  </ul>

  <!-- Setting the submit name to "btn" -->
  <input type="submit" value="submit" name="btn" id="btn-submit"/>
  </form>
  </div><!--/#selectnews-->

The only real thing worth noting in the form code are the names of the checkboxes. We use all the same name to group them together and the [ ] helps create the array on submit. I just chose to use source[] because it made sense to me, but you can use anything as long as the names are consistent and the brackets are there. Just adjust your PHP accordingly.

SimplePie PHP
<?php
/* Get the SimplePie library */
require_once ('inc/simplepie.inc');

/* Create a new instance of SimplePie */
$feed = new SimplePie();

/* Get array */
// If the form has been submitted and the user selected news sources
if (isset ($_POST['btn']) && isset($_POST['source'])) {

    // Save the array of news sources to a variable $checked
    $checked = $_POST['source'];

    // Pass the array of news sources through the SimplePie set_feed_url() function
    $feed->set_feed_url($checked);

    // Also, save this data to a cookie called "feedurls" that will expire when the browser closes
    setcookie('feedurls', serialize($checked));

// If the form wasn't submitted look for the "feedurls" cookie
} else if (isset ($_COOKIE['feedurls'])) {

	// If the cookie was found, pass the data through set_feed_urls()
	$feed->set_feed_url(unserialize($_COOKIE['feedurls']));
}

/* Set item limit */
$feed->set_item_limit(3);

/* Enable caching */
$feed->enable_cache(true);

/* Provide the caching folder */
$feed->set_cache_location('cache');

/* Set the amount of seconds you want to cache the feed */
$feed->set_cache_duration(1800);

/* I don't know what "init" means */
$feed->init();

/* Handle the content type (atom, RSS...) */
$feed->handle_content_type();
?>

This is nothing that can’t be read in the comments, but the guts of the PHP comes in right under the “Get Array” comment. We’re checking to see if the form was submitted and the user selected news sources; if so, dump the values into SimplePie and save them to a cookie we can refer to later. The initial idea was for this to live at a pretty open place (a computer lab or something), that’s why the cookie expires on browser close.

I limited the per-feed output to 3 to keep it a little clean, but you can set that to whatever you want. It’d be pretty easy to let the user choose this as well with a select menu:

<select name="displayNumber">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>

And replace $feed->set_item_limit(3); with $feed->set_item_limit($_POST['displayNumber']);. There may be some extra tweaking beyond that, but the general idea is to pass the submitted value into SimplePie.

jQuery

Now that we have the form fully functional we can layer some JavaScript on top of it to make it a little more nifty. This is using the jQuery Form Plugin. It’s the first time I’ve really used this plugin, but it’s actually pretty nice and easy to use. I had some little quirks with it, but nothing too major.

$(function() {

/* If JavaScript is active create a link to slide the form out */
$('#getform a').click(function(){
	$('#selection').slideToggle('slow');
	return false;
});//

/* make it look like something happened */
$('#selection, #reader').hide(); //
$('#reader').fadeIn('slow'); //

 /* bind form using 'ajaxForm' */
$('#selection').ajaxForm(options); 

/* Submit form via Ajax */
var options = {
    target:       '#rssloader',
}; //

});// End jQuery

View demo

Download demo files

Anyway, that’s all I have for today. It’s a pretty unpolished demo so let me know what you think, where I screwed up and if you can build on it and make it better. I’ll try to write some more soon.

Ingredient list

Tags: , , , ,
Posted in Web Development | 6 Comments »

Where I Get My News

Friday, January 16th, 2009

article image

This morning it occurred to me that I haven’t written a post about my RSS feeds. I’ve been meaning to for about a year now, but for whatever reason it hasn’t happened until now. I rely on my feed reader for a lot of inspiration so I feel like I should share that and maybe promote some great sites at the same time.

I Break my feeds into 2 categories: Work & Fun

Work

Fun

So, that’s all of them. Did I miss any big ones? Let me know if there’s some great resource we should all be reading.

Tags:
Posted in News, Web Development | 8 Comments »

Keeping up with the times

Friday, August 29th, 2008

Everytime you turn around there’s something new in the web to learn. Whether it be another JQuery plugin (YAJPI), a new blogging service (YABS), social networking site (YASNS) a new way to handle CSS Sprites or yet another acronym to learn (YAATL).  There’s so much to do and learn about, how do we keep up?

I think most of us (the people who read this blog) keep up with the times by monitoring RSS feeds or reading books, I know that’s how I do it. But over the past few years my RSS reader has become more and more bloated. From time to time you may run through your feeds and delete the older ones, one’s that haven’t posted in a while or people who just complain about the government. But they’re all still valuable!

I try very hard to never delete an RSS feed. In fact, not too long ago I noticed that my feed menu had started to run off the page (I use Firefox to manage my RSS), so I got a bigger monitor to compensate; because you never really know where that bit of genius, that air of brilliance will come from. It could be from Eric Meyer, David Shea or from your random Swatkins blog.

I first noticed that I was neglecting my feeds when I saw 2 cycles of A List Apart and at least 10 CSS-Tricks posts/screencasts whiz past me, and that I hadn’t made a blog post in over a month (not cool with that at all). We’re all very busy at work, some more than others, but we still need to keep up with what’s happening in the industry.

Many times when I’m busy and I look at that long list of RSS feeds in front of me I just let out a :sigh: and say “I’ll just check a couple since I’m busy, and then maybe some more tomorrow when it calms down.” It had been happening a lot lately so I came to the conclusion that it’s not going to calm down, and that it’s time to get back into my old RSS groove. It’s really not that difficult to take some time out of every day for a little news reading. In fact, the ALA news days usually take the most time.

Many of us get to work, have our coffee, sit down, chat a little and before you know it, 20-30 minutes have gone by and you haven’t really “started” work yet. This is the time I use to read my news.

Not every blog/site you follow will update content on the same day. If you run through them every morning before you start your “heads-down” development/design you’ll find 1, maybe 2 sites that have made updates.

Keeping up with your RSS is really important, especially if your one of those psycho/learn everything about everything Web designer (like me :o P). I find that at least checking your feeds once a day, or even every other day can save you tons of time, energy and frustration in the long run.

But that’s just me hoping that I haven’t lost too many readers because of my month off :o ).

How do YOU keep up with your news?

Tags: , ,
Posted in Life, Web Development | 2 Comments »

Cracking open Google Gadgets

Monday, April 14th, 2008

A few weeks ago, I decided to do a little research into Google Desktop. Which is basically a Google widget platform competing with Yahoo! widgets. After some exploration and dissecting Google Gadgets, this is what I came out with:

  • The core of the application runs on 2 files
    • main.js
    • main.xml
  • There are also some image and CSS options readily available, but the guts of it really run on JavaScript and XML
  • Overall, it’s very straightforward, a little playing around with the sample gadgets will give you a good idea of what you can do (endless possibilites)

The file structure of some of the sample gadgets leaves a lot of room for improvement. Some seem to be built to handle very large, modular applications, but in most cases, they’re a little too modular (in my opinion).

Some of the more useful samples you can go through are:

  • Animations
  • ComboBox
  • Countdown
  • DragDrop
  • HelloWorld
  • HTMLDetailsView
  • QueryAPI
  • RSS
  • XMLDetailsView
  • XMLHttpRequest

There are others too, but these are the one’s I found most useful. So, lookin at those options, a being an RSS freak, I chose to customize the RSS gadget for CSSKarma

CSSKarma RSS Gadget

Download CSSKarma RSS Gadget

The first thing to do is to make a copy of the RSS sample given with the Google Gadget samples. I did that (as a CSS person), I opened up the theme folder “default” and looked for “details.css”. I assumed this file would edit all the presentation for the gadget. It doesn’t. I only edits the flyout menu (after you click on a link in the RSS gadget). OK, that’s fine. So I enter this CSS:

body {
  color:#323F4C;
  background:#fff url(http://www.csskarma.com/images/bg_branding.jpg) no-repeat 0 bottom;
  font: 0.8em/1.5 Verdana, Arial, Sans-serif;
}

That will add my CSSKarma logo to the flyout and add some basic CSS. The other files in the theme folder to deal with are “entry_item.xml”, “theme_config.xml” and “title.xml”. After you look at “theme_config.xml” and “title.xml”, you’ll see why I think this is a little too modular. I left those two file alone. But I did edit “entry_item.xml”, view entry_item.xml. I edited some style information in there (one more thing I’m not thrilled about – embedding style info into the XML).

The next folder to tackle is “editme”. It contains 2 files (config_constants.js and entry_item_impl.js). In “entry_item_impl.js” you can edit the RSS nodes, if you want. I chose not to. In “config_constants.js” you need to edit the URL of the RSS feed you want to use.

The last thing I wanted to do was to remove the default background image from “main.xml”. When I did this, the background was transparent, by default. So in “main.xml” I added a background attribute of #FFFFFF to top level div (container). I also edited the height and width attributes of the “view” element for when the gadget is undocked from Google Desktop.

The outcome of this was the CSSKarma RSS Gadget (You’ll probably need Google Desktop installed to use this). A very basic RSS gadget made by making simple modifications to the sample files. Feel free to download, play around with it, and let me know about any errors you find.

Aptana Jaxter

Aptana Jaxter recently released “The worlds first Ajax server” (I have no date to back up the claim that it was recent). I’ve seen some web apps using this in integrating the Google API, looks promising but there’s very limited server space right now. I’d like to see how these can be integrated into the Mobile web. I don’t have too much information on it right now (and I keep losing the damn link).

That’s all I have for now.

With ease
Tim

Tags: , , , , , ,
Posted in Web Development | 2 Comments »

|

New from the blog

Are My Sites Up? authenticjobs.com