Tag Class
The Tag class contains functions that give you access to the parameters passed in tags. This class is designed to behave similarly to the URI Class.
Note: This class is initialized automatically by the system so there is no need to do it manually.
Why a Tag Class?
You might expect tag parameters to be passed directly to the tag (controller) functions as parameters. In other words, you you might expect that this tag:
<?php get('core.view.load', 'header', $data, TRUE); ?>
would map to the controller function like this:
class View extends Controller {
function load($view, $vars, $return)
{
...
}
}
CodeIgniter, however, automatically passes URI segment values to controller functions as parameters, so CoolBrew had to take another approach. You must specifically retrieve the parameters using the Tag class:
class View extends Controller {
function load()
{
$view = $this->tag->param(1);
$vars = $this->tag->param(2, array());
$return = $this->tag->param(3, FALSE);
if ( ! $view)
{
show_error('core.view.load: the file to view was not specified.');
}
...
}
}
This way, you can still retrieve URI segments as function parameters in your CoolBrew tags. For more information, see Passing URI Segments to your Functions in the CodeIgniter User Guide.
$this->tag->param(n)
Permits you to retrieve a specific tag parameter. Where n is the parameter number you wish to retrieve. Parameters are numbered from left to right. For example, if your tag is this:
<?php get('peanuts.snoopy', 'supper_dish', 'lila', 'round_headed_kid'); ?>
The parameter numbers would be this:
- supper_dish
- lila
- round_headed_kid
By default the function returns FALSE (boolean) if the tag parameter does not exist. There is an optional second $this->tag->param() parameter that permits you to set your own default value if the tag parameter is missing. For example, this would tell the function to return the number zero in the event of failure:
$product_id = $this->tag->param(3, 0);
It helps avoid having to write code like this:
if ($this->tag->param(3) === FALSE)
{
$product_id = 0;
}
else
{
$product_id = $this->tag->param(3);
}
$this->tag->param_array()
Returns an array containing the tag parameters. For example:
$params = $this->tag->param_array();
foreach ($params as $parameter)
{
echo $parameter;
echo '<br />';
}
$this->tag->total_params()
Returns the total number of parameters.