Error message

Warning: ini_set(): A session is active. You cannot change the session module's ini settings at this time in drupal_environment_initialize() (line 697 of /home/adaptive/public_html/includes/bootstrap.inc).

Working with Template Variables

This is the second article in the series of Packpub Drupal theming articles from their new book Drupal 6 Themes—**Working with Template Variables in Drupal 6**.

As we have seen in the first article *[Creating Dynamic CSS Styling](http://adaptivethemes.com/creating-dynamic-css-styling)*, Drupal produces variables that can be used to enhance the functionality of themes. Typically, a theme-related function returns values reflecting the state of the page on the screen. A function may indicate, for example, whether the page is the front page of the site, or whether there are one, two, or three active columns (for example, the variable $body_classes). Tapping into this information is a convenient way for a theme developer to style a site dynamically.

The default Drupal variables cover the most common (and essential) functions, including creating unique identifiers for items. Some of the Drupal variables are unique to particular templates; others are common to all. In addition to the default variables, you can also define your own variables.

> Using the function theme_preprocess(), you can either set new variables, or unset existing ones that you do not want to use.

In Drupal 6, preprocess functions have made working with variables easier and cleaner. By using the preprocessor, you can set up variables within your theme that can be accessed by any of your templates. The code for the preprocess function is added to your template.php file, thereby keeping the actual template files (the .tpl.php files) free of unnecessary clutter.

Note that the preprocess functions only apply to theming hooks implemented as templates; plain theme functions do not interact with the preprocessors.

In Drupal 5 and below, the function _phptemplate_variables served the same purpose as the preprocess function. For a list of the expected preprocess functions and their order of precedence, see [http://drupal.org/node/223430](http://drupal.org/node/223430).

Typically, if you wish to implement a preprocessor applicable to your theme, you will use one of the following:

Name and Application of Preprocessor Functions
Name of preprocessor Application
[engineName]_preprocess This namespace should be used for your base theme. Should be named after the theme engine used by the theme. Will apply to all hooks.
[engineName]_preprocess_ [hookname] Should be used for your base theme. Also named after the theme engine applicable to the theme but note that it is specific to a single hook.
[themeName]_preprocess This namespace should be used for subthemes. Will apply to all hooks.
[themeName]_preprocess_ [hookname] Should be used for subthemes. Note that it is specific to a single hook.

**Let's look first at intercepting and overriding the default variables and then at creating your own variables.**

## Intercepting and Overriding Variables

You can intercept and override the system's existing variables. Intercepting a variable is no different in practice from intercepting a themable function: you simply restate it in the template.php file and make your modifications there, leaving the original code in the core intact.

To intercept an existing variable and override it with your new variable, you need to use the function _phptemplate_preprocess(). Add this to your template.php file according to the following syntax:

<?php
function phptemplate_preprocess(&$vars) {
$vars['name'] = add your code here...;
}
?>

Note that nothing should be returned from these functions. The variables have to be passed by reference, as indicated by the ampersand before variables, e.g., &$vars.

Let's take a very basic example and apply this. Let's override $title inpage.tpl.php. To accomplish this task, add the following code to the template.php file:

<?php
 
function phptemplate_preprocess(&$vars) {
 
$vars['title'] = 'override title';
}
?>

*Remember to clear your theme registry!*

With this change made and the file saved to your theme, the string override title will appear, substituted for the original $title value.

## Making New Variables Available

The preprocess function also allows you to define additional variables in your theme. To create a new variable, you must declare the function in the template.php file. In order for your theme to have its preprocessors recognized, the template associated with the hook must exist inside the theme. If the template does not exist in your theme, copy one and place it in the theme directory.

The syntax is the same as that just used for intercepting and overriding a variable, as seen above. The ability to add new variables to the system is a powerful tool and gives you the ability to add more complex logic to your theme.

## Summary

In this two part article we covered the basics needed to make your Drupal theme responsive to the contents and the users. By applying the techniques discussed here, you can control the theming of pages based on content, state of the page or the users viewing them. Taking the principles one step further, you can also make the theming of elements within a page conditional. The ability to control the templates used and the styling of the page and its elements is what we call dynamic theming.

We covered not only the basic ideas behind dynamic theming, but also the techniques needed to implement this powerful tool. Among the items discussed at length were the use of suggestions to control template display, and the implementation of $body_classes. Also covered in this article, was the use of the preprocess function to work with variables inside your theme.

Drupal 6 Themes

Create new themes for your Drupal 6 site with clean layout and powerful CSS styling

  • Learn to create new Drupal 6 themes
  • No experience of Drupal theming required
  • Techniques and tools for creating and modifying themes
  • A complete guide to the system's themable elements

http://www.packtpub.com/drupal-6-themes/book

Last updated 14th April, 2009 - 10:39pm

Authored by Jeff Burnz on