Drupal 6 theming : preprocessing function

Posted by: 
Dominique De Cooman

Use this function if you want to pass extra variables to a tpl.php file.
Forexample :
You want to pass a variable to page.tpl.php. Put in template.php this function.

function phptemplate_preprocess_page(&$variables) {
  $variables['my_var'] = '<span>this is my var</span>';
}

In your page.tpl.php $my_var will be available.

If you call it without page in the function $my_var will be available in every theme function that is being called.

function phptemplate_preprocess(&$variables) {
  $variables['my_var'] = '<span>this is my var</span>';
}

The lower functions can override the above functions.
The hierarchy of the functions :

 
template_preprocess()
template_preprocess_page()
yourmodulename_preprocess()
yourmodulename_template_preprocess_page()
phptemplate_preprocess()
phptemplate_preprocess_page()
yourthemename_preprocess()
yourthemename_preprocess_page()

More on theming can be found on http://api.drupal.org/api/function/theme/6

Comments

Drupal 6 theming : preprocessing function

Thanks for the tip! :)

Add new comment