Thursday, July 7, 2011

Making additional variables available to your templates

function _phptemplate_variables($hook, $vars) {
switch(
$hook) {
case
'comment' :
$vars['newvar'] = 'new variable';
$vars['title'] = 'new title';
break;
}
return
$vars;
}
?>

The $hook refers to the area the variable is to be used in (e.g. for comment.tpl.php, it would be "comment").

This function needs to be defined in a template.php file, which is placed inside the template directory (for instance : themes/box_cleanslate/template.php)


The output of this function is merged with the variables returned from phptemplate_comment, so you can easily adjust whichever variables you feel necessary.

Your comment.tpl.php file will now have a new variable available in it
called $newvar. Similarly the $title variable will be overridden with the value specified in
the function.

No comments:

Post a Comment