Tag Cloud

Weighted tag clouds can be build using list_subtopics with nested list_articles. This need be nested several times, as deep you topics tree is.

Example for an 4-level topics tree:

{{ list_subtopics }}
  {{ list_articles length=1 }}
    {{ set_section number="150" }}
    {{ $campsite->url->reset_parameter('tpl') }}
        <font size="{{ $campsite->current_list->count }}">
        <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
        </font>
        &nbsp;&nbsp;
  {{ /list_articles }}
  {{ list_subtopics }}
    {{ list_articles length=1 }}
      {{ set_section number="150" }}
      {{ $campsite->url->reset_parameter('tpl') }}
          <font size="{{ $campsite->current_list->count }}">
          <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
          </font>
          &nbsp;&nbsp;
    {{ /list_articles }}
    {{ list_subtopics }}
      {{ list_articles length=1 }}
        {{ set_section number="150" }}
        {{ $campsite->url->reset_parameter('tpl') }}
            <font size="{{ $campsite->current_list->count }}">
            <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
            </font>
            &nbsp;&nbsp;
      {{ /list_articles }}
      {{ list_subtopics }}
        {{ list_articles length=1 }}
          {{ set_section number="150" }}
          {{ $campsite->url->reset_parameter('tpl') }}
              <font size="{{ $campsite->current_list->count }}">
              <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
              </font>
              &nbsp;&nbsp;
        {{ /list_articles }}
      {{ /list_subtopics }}
    {{ /list_subtopics }}
  {{ /list_subtopics }}
{{ /list_subtopics }}

The usage count of an tag is made visible by using the articles count as font-size. Regardless the list_articles have parameter length=1, the $campsite->current_list->count returns the full count of found articles.

Caching

The nested lists will consume lot of performance, in dependency of number of topics and number of articles. The following example implements an filesystem cache. The cloud will be recrated after configurable time.

{{ php }}
$cachefile = CS_PATH_SITE.DIR_SEP.CS_PATH_SMARTY_TEMPLATES.'/skins/greenpiece/cache/tag-cloud.tpl';
$lifetime = 3600; //lefetime in seconds
if (!file_exists($cachefile) || time() - filectime($cachefile) >= $lifetime) {
  if (!is_writable($cachefile)) {
    echo "Unable to write to cache file $cachefile<br>";
  }
  $fp=fopen(CS_PATH_SITE.DIR_SEP.CS_PATH_SMARTY_TEMPLATES.'/skins/greenpiece/cache/tag-cloud.tpl', 'w+');
  ob_start();
  {{ /php }}
  {{ list_subtopics }}
    {{ list_articles length=1 }}
      {{ set_section number="150" }}
      {{ $campsite->url->reset_parameter('tpl') }}
          <font size="{{ $campsite->current_list->count }}">
          <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
          </font>
          &nbsp;&nbsp;
    {{ /list_articles }}
    {{ list_subtopics }}
      {{ list_articles length=1 }}
        {{ set_section number="150" }}
        {{ $campsite->url->reset_parameter('tpl') }}
            <font size="{{ $campsite->current_list->count }}">
            <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
            </font>
            &nbsp;&nbsp;
      {{ /list_articles }}
      {{ list_subtopics }}
        {{ list_articles length=1 }}
          {{ set_section number="150" }}
          {{ $campsite->url->reset_parameter('tpl') }}
              <font size="{{ $campsite->current_list->count }}">
              <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
              </font>
              &nbsp;&nbsp;
        {{ /list_articles }}
        {{ list_subtopics }}
          {{ list_articles length=1 }}
            {{ set_section number="150" }}
            {{ $campsite->url->reset_parameter('tpl') }}
                <font size="{{ $campsite->current_list->count }}">
                <a href="{{ uri }}">{{ $campsite->topic->name }}</a>
                </font>
                &nbsp;&nbsp;
          {{ /list_articles }}
        {{ /list_subtopics }}
      {{ /list_subtopics }}
    {{ /list_subtopics }}
  {{ /list_subtopics }}
  {{ php }}
  echo "n<!-- file generated ".date('Y-m-d H:i:s')." -->";
  fwrite($fp, ob_get_contents());
  ob_end_clean();
}
readfile($cachefile); 
{{ /php }}