You are here: Home > Articles > Article Display

Firefox download counter in Coldfusion

We'll see how to create and use a Coldfusion custom tag that will make it easy for us to display the Firefox download counter.

Published: Jun 21, 2005
Tested with: Coldfusion 6.1
Category: ColdFusion
8,582 views

Introduction

Over at the spreadfirefox website they explain how to syndicate their Firefox download counter with different kinds of programming languages. At the time of writing this article though, there was no sample code for doing that in Coldfusion. In this article we'll see how to do that ourselves, by first taking a closer look at this counter, and then we'll create a custom tag to make it easier to display in a Coldfusion page.

The Firefox download counter RSS feed

The actual RSS link they offer leads to this file (as seen through a browser):

Firefox Xml file

As you can see their feed is a simple RSS file with some basic information at the top and the part that interests us is found under the item node, in description. In the screen capture above, we can see that the download is at 65,381,753. The counter is updated every minute.

Using the custom tag

I created a file named (obviously enough) "firefox_counter.cfm", which will act as the custom tag. Since this article is not about how to create custom tags, I will assume that you know how to add this file as part of your site's custom tags. If not, you can place it under the same folder of the file that you call it from. You can find the tag in the downloadable materials for this article.

Using the tag is very easy:

1 <cf_firefox_counter / >

Or:

1 <cf_firefox_counter>#count#</cf_firefox_counter>

The variable count is a reserved word I created inside the tag to output the download count. You can obviously wrap this word into anything you like inside the tag, like additional text or images. For example:

1 <cf_firefox_counter>The current Firefox download counter is at <strong>#count#</strong>!</cf_firefox_counter>

Passing optional parameters to the custom tag

This tag has 7 optional parameters:

Name Default Description
CacheTimeUnit h

The unit of time measurement to be used for caching. These are the same values used for Coldfusion's DateAdd function, and possible values are:

  1. yyyy: Year
  2. q: Quarter
  3. m: Month
  4. y: Day of year
  5. d: Day
  6. w: Weekday
  7. ww: Week
  8. h: Hour
  9. n: Minute
  10. s: Second
  11. l: Millisecond

Must be used together with CacheTime.

CacheTime 24 A number that combined with the CacheTimeUnit parameter will determine the caching interval.
Must be used together with CacheTimeUnit.
Directory /firefox/ The location where the XML RSS file will be saved at. Default is in the firefox directory, located under the website root. Path must end with a "/". Directories will not be created on the fly, rather they must already exist on the server.
Filename firefox_download_counter The name that will be given to the XML file written on your server. Do not include the xml extension, just the filename.
Output commas

The type of output of the count. There are 3 different types:

  1. plain (i.e. "65381753")
  2. commas (i.e. "65,381,753")
  3. images: numbers and commas will be replaced with images. Must be used together with ImagesDirectory and ImagesType.
ImagesDirectory /firefox/images/ Url path to the images location. Default is in the images directory, located under the firefox directory. Path must end with a "/".
Must be used together with Output and ImagesType.
ImagesExtension gif Extension of images to be used.
Must be used together with Output and ImagesDirectory.

Caching is very important when using this tag. Obviously you do not want to be making HTTP requests on every page load, especially since the live counter is updated only every minute. The way I have implemented caching inside my code, is to use the Last Modified Date of the file on the disk to compare it to the caching setting of the tag and decide whether to do an HTTP request or not. The default caching is set to one day. Here's an example of using the custom tag with all 7 parameters:

1 <cf_firefox_counter CacheTimeUnit="m" CacheTime="15" Directory="/firefox/" Filename="firefox" Output="images" ImagesDirectory="/firefox/numbers/" ImagesExtension="jpg">#count#</cf_firefox_counter>

The tag can produce the counter as plain text, text with commas, or as images. Choosing to produce an image output will result in each letter and comma to be replaced by its corresponding image:

Custom Tag example outputing images

You can create your own images for JPG or PNG format, or replace the ones I provide in the downloadable code for the GIFs. Just name your images after the number they represent (from 0 to 9), and an additional one called "comma" for the comma separator.

Creating the custom tag

I will spare you the details in this article of the internal complexities of the tag. For those of you interested, you can take a look at it once you download it. :o) The tag itself is very simple:

  1. It verifies that a closing tag exists
  2. Gathers and checks all parameters
  3. Checks the cache update to see if it needs refreshing
  4. Downloads and writes to disk the new XML file (if needed). Also strips the DOCTYPE line from the XML file, to avoid validation roundtrips when processing the file
  5. Processes the saved file and outputs the needed output

Conclusion

The method I chose for caching the feed is not of course the only one, or maybe even the best one, but I think it works rather well. Feel free to change my code as you see fit and please report back to me your findings.

 



Other articles in this category
  1. A ColdFusion weather custom tag
    September 15, 2005
    This custom tag will allow you to easily add a weather control to your website. Using the XML data feeds from weather.com we can produce owesome looking weather controls, both for current conditions and for forecasts. This control has a multitude of attributes, is fully customizable, easy to use, comes with example templates, and full source code provided.
  2. A Coldfusion color picker custom tag
    June 27, 2005
    We'll learn how to create and use a Coldfusion custom tag, that will add a color picker field to a form and make it easy to select a web safe color. No popups were harmed during the making of this article!
  3. Working with cfpop email headers
    March 31, 2005
    Using the cfpop tag to retrieve emails returns the headers as one long text string, which makes it difficult to get specific values. In this article we'll see first how to get the headers, and then how to work with them to get the name-value pair we want.