Passing Extra Variables to Templates

The variables listed for each of the previous tpl.php template files are all you need to support the normal functioning of your Drupal site. In case you would like to push other information into the template files, there is a mechanism for adding variables. The following function must be added to a file named template.php in your theme's directory:

function _phptemplate_variables($hook, $vars) { return $vars;

For Bluemarine, this would be in the file themes/bluemarine/template.php. The $hook variable is the name of the themable function being called and subsequently the root of the name of the template file to which the variables are headed. The $vars variable is an associative array containing the default variables as described in the preceding section. Thus, when the theme('block') function is called, _phptemplate_variables will also be called with the parameters 'block' and an array of the variables associated with the block.tpl.php template: $block, $id, $block_id, $zebra, $block_zebra, and $is_front. At this point, you have the chance to add values to the array, or even change those that are already inside it. The $vars array that you return will determine which variables are visible to the block.tpl.php file. The same is true for each of the default tpl.php files, as well as any other template files that you may have added, such as node-type-specific files or overrides of themable functions. Exercise 5-2 demonstrates how to pass extra variables to a template.

0 0

Post a comment