Zend\Tag\Cloud is the rendering part of Zend\Tag. By default it comes with a set of HTML decorators, which allow you to create tag clouds for a website, but also supplies you with two abstract classes to create your own decorators, to create tag clouds in PDF documents for example.
You can instantiate and configure Zend\Tag\Cloud either programatically or completely via an array or an instance of Zend\Config\Config. The available options are:
Using Zend\Tag\Cloud
This example illustrates a basic example of how to create a tag cloud, add multiple tags to it and finally render it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Create the cloud and assign static tags to it
$cloud = new Zend\Tag\Cloud(array(
'tags' => array(
array('title' => 'Code', 'weight' => 50,
'params' => array('url' => '/tag/code')),
array('title' => 'Zend Framework', 'weight' => 1,
'params' => array('url' => '/tag/zend-framework')),
array('title' => 'PHP', 'weight' => 5,
'params' => array('url' => '/tag/php')),
)
));
// Render the cloud
echo $cloud;
|
This will output the tag cloud with the three tags, spread with the default font-sizes.
The following example shows how create a tag cloud from a Zend\Config\Config object.
1 2 3 4 5 6 7 8 9 10 | # An example tags.ini file
tags.1.title = "Code"
tags.1.weight = 50
tags.1.params.url = "/tag/code"
tags.2.title = "Zend Framework"
tags.2.weight = 1
tags.2.params.url = "/tag/zend-framework"
tags.3.title = "PHP"
tags.3.weight = 2
tags.3.params.url = "/tag/php"
|
1 2 3 4 5 6 | // Create the cloud from a Zend\Config\Config object
$config = Zend\Config\Factory::fromFile('tags.ini');
$cloud = new Zend\Tag\Cloud($config);
// Render the cloud
echo $cloud;
|
Zend\Tag\Cloud requires two types of decorators to be able to render a tag cloud. This includes a decorator which renders the single tags as well as a decorator which renders the surounding cloud. Zend\Tag\Cloud ships a default decorator set for formatting a tag cloud in HTML. This set will by default create a tag cloud as ul/li-list, spread with different font-sizes according to the weight values of the tags assigned to them.
The HTML tag decorator will by default render every tag in an anchor element, surounded by a li element. The anchor itself is fixed and cannot be changed, but the surounding element(s) can.
Note
URL parameter
As the HTML tag decorator always surounds the tag title with an anchor, you should define an URL parameter for every tag used in it.
The tag decorator can either spread different font-sizes over the anchors or a defined list of classnames. When setting options for one of those possibilities, the corespondening one will automatically be enabled. The following configuration options are available:
The following example shows how to create a tag cloud with a customized HTML tag decorator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | $cloud = new Zend\Tag\Cloud(array(
'tagDecorator' => array(
'decorator' => 'htmltag',
'options' => array(
'minFontSize' => '20',
'maxFontSize' => '50',
'htmlTags' => array(
'li' => array('class' => 'my_custom_class')
)
)
),
'tags' => array(
array('title' => 'Code', 'weight' => 50,
'params' => array('url' => '/tag/code')),
array('title' => 'Zend Framework', 'weight' => 1,
'params' => array('url' => '/tag/zend-framework')),
array('title' => 'PHP', 'weight' => 5,
'params' => array('url' => '/tag/php')),
)
));
// Render the cloud
echo $cloud;
|
The HTML cloud decorator will suround the HTML tags with an ul-element by default and add no separation. Like in the tag decorator, you can define multiple surounding HTML tags and additionally define a separator. The available options are:
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.