Child pages
  • Build a Cached Tag Cloud
Skip to end of metadata
Go to start of metadata

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:

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

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

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

      {{ list_subtopics }}
        {{ list_articles length=1 }}
          {{ set_section number="150" }}
          {{ $gimme->url->reset_parameter('tpl') }}
              <font size="{{ $gimme->current_list->count }}">
              <a href="{{ uri }}">{{ $gimme->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 $gimme->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.

#!css+smarty
{{ 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" }}
      {{ $gimme->url->reset_parameter('tpl') }}
          <font size="{{ $gimme->current_list->count }}">
          <a href="{{ uri }}">{{ $gimme->topic->name }}</a>
          </font>
          &nbsp;&nbsp;
    {{ /list_articles }}

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

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

        {{ list_subtopics }}
          {{ list_articles length=1 }}
            {{ set_section number="150" }}
            {{ $gimme->url->reset_parameter('tpl') }}
                <font size="{{ $gimme->current_list->count }}">
                <a href="{{ uri }}">{{ $gimme->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 }}

  • No labels