Code snippets for symfony 1.x

Navigation

Refine Tags

Snippets tagged "tags"

Create Automatic Canonical Link Tags

<link rel="canonical" href="<?php echo url_for($sf_context->getRouting()->getCurrentInternalUri(true), true); ?>" />
 
by Erik Osterman on 2009-02-19, tagged canonical  link  tags 
(1 comment)

Tag cloud helper

This is a simple helper to create a tag cloud from an array of tags.

Creating a tag cloud is as easy as calling the helper:

<?php
  use_helper('Tagcloud');
  TagcloudHelper::showCloud($tags);
 

Where tags is an array that is defined in the following syntax:

  array('tag1' => 10, 'tag2' => 15, 'tag3' => 4); 
 

The values in this array are deciding the weight of your tag. It's usually the amount of posts that have this tag, but could also be determined in your own way. As long as the passed array has this syntax, your tag cloud will be built.

This tagcloud will use a piece of css that must be present in your css file:

.cloud_xsmall {
  font-size: 80%;
}
 
.cloud_small {
  font-size: 100%;
}
 
.cloud_medium {
  font-size: 120%;
}
 
.cloud_large {
  font-size: 140%;
}
 
.cloud_xlarge {
  font-size: 160%;
} 
 

Obviously, this piece of css can easily be adapted to suit your needs.

Be sure that there is a route in your routing.yml that can handle the url /tag/:tag

The actual Helper:

<?php
 
class TagcloudHelper
{
 
  /**
   * Generate the tag array and echo the cloud
   * 
   * @return array
   *
   */
  public function showCloud($tags) {
    asort($tags);
    reset($tags);
    $this->smallest = current($tags);
    end($tags);
    $this->largest = current($tags);
    $diff = $this->largest - $this->smallest;
    $this->interval = round($diff/3);
 
    reset($this->tags);
 
    $this->cloud_tags = array();
    foreach($this->tags as $tag => $amount) {
      echo link_to($tag, 'tag/' . urlencode($tag), array('class' => $this->getSize($amount))) . ' ';
    }
  }
 
  /**
   * Get the size (css class) based on the amount
   *
   * @param integer $amount
   * @return array
   */
  private function getSize($amount) {
    if ($amount == $this->smallest) {
      return 'cloud_xsmall';
    }
    if ($amount == $this->largest) {
      return 'cloud_xlarge';
    }
    if ($amount > $this->smallest + (2*$this->interval)) {
      return 'cloud_large';
    }
    if ($amount > $this->smallest + $this->interval) {
      return 'cloud_medium';
    }
    return 'cloud_small';
  }
 
} 
 
by Stefan Koopmanschap on 2007-10-28, tagged display  tagcloud  tags