raw code

Tag Cloud plugin for Smarty

Robert Eisele

I've written a featureful plugin for the Smarty templating engine to be able to generate tag clouds very easily. Smarty by itself is a really slow engine, so that it would not the worst idea to use a plugin which is written in pure PHP instead of generating the list of words with the even slower {math} extension of Smarty.

Download the Smarty plugin

To use the plugin, you need to place the PHP file into your smarty plugins directory. In your PHP script, do something like this:

<?php

$tags = array(
  array('tag' => 'Robert', 'num' => 55, 'link' => 'robert'),
  array('tag' => 'raw', 'num' => 66, 'link' => 'raw'),
  array('tag' => 'Open Source', 'num' => 41, 'link' => 'open-source'),
  array('tag' => 'PHP', 'num' => 90, 'link' => 'php'),
  array('tag' => 'Smarty', 'num' => 14, 'link' => 'smarty'),
  );

$smarty->assign_by_ref('tags', $tags);

?>

In the template you're display()ing or fetch()ing, you can now use the new {cloud} tag, like so:

{cloud tags=$tags minsize=6 maxsize=25 sort="random" limit=50 url="/tag/%link%/" class="tags tag-%size%" font="%size%px"}

The example of above could generate an output similar to this:

The new function is able to handle the following parameters:

<a href="/tag/%link%/" class="tags tag-%size%">%tag%</a>

The size of the %size% parameter is ever an integer in the interval [minsize, maxsize]. This way it's possible to is it as class-name extension as well as a font-size CSS property. Note, that for class names, the lowest number is also the smallest tag.