O'Reilly logo

Building Tag Clouds in Perl and PHP by Jim Bumgardner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Displaying Tags In Perl Using Your Own Code

There are various ways to display a tag cloud. I've chosen a style that closely resembles the tag clouds on Flickr. Here is the HTML for a very small tag cloud, so you can see how it is structured:

<div class="cdiv">
<p class="cbox">
<a href="link1" style="font-size:23px;">tag1</a>
<a href="link2" style="font-size:18px;">tag3</a>
<a href="link3" style="font-size:13px;">tag3</a></p>
</div>

Each word or tag is associated with the style division class cdiv (which is defined in the CSS style file), and the font size is given explicitly for each tag.

These days, it is fashionable to separate style from structure, and keep all stylisticinformation in the CSS file. The tag clouds produced by HTML::TagCloudaccomplish this goal by eliminating the explicit font-size references and using a setof individual styles (tagcloud1, tagcloud2, tagcloud3, and so on), onefor each font size. While the basic idea of separating style from structure isdesirable, this particular use strikes me as silly, since the separate classes arefunctioning as implicit font-size directives. It reduces clarity in the HTML codeand makes the CSS code needlessly complex.

Since we have full control over the code that generates the tag cloud, there is littleneed to use CSS to modify the range of font sizes—instead, we will control thisdetail through scripting.

For the tag clouds in this article, I am putting the font-size directive in the tagcloud code itself, and using a shorter CSS ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required