Drupal 6 Template Variables - Going to the Source

The addition of preprocessor functions to Drupal 6 makes customizing template variables straightforward and clean (bravo to everyone who made this happen!). Have you ever wondered where the variables come from? It's actually helpful knowing, even if you're a themer with basic PHP knowledge.

Some reasons why:

  • Seeing how the the variable is created allows you to make an informed decision on the best way of changing it. e.g., Modifying the theme function vs overriding the value.
  • In the case of a module, knowing the logic behind how a variable is created can tell you whether or not a Drupal function should be used to add the information to the theming layer.
    e.g. $head / drupal_get_html_head() / drupal_set_html_head()
  • Often a theme function is used to generate the variable. Copying the technique can simplify your code.
  • Knowing the logic behind how a variable is created can aid troubleshooting.

In Drupal 6 the variable creation logic lives in the /includes/theme.inc file. The functions of note, located at the bottom of the file, are:

By browsing the function definitions you'll see how the variables are created.

For example, perhaps you're not happy with the way taxonomy terms are formatted in the $terms variable. You've decided to create your own variable, $fancy_terms, with customized formatting. Checking the template_preprocess_node function and seeing how it uses both:

$variables['taxonomy'] = taxonomy_link('taxonomy terms', $node);

and

$variables['terms'] = theme('links', $variables['taxonomy'], array('class' => 'links inline'));

to retrieve and format the taxonomy information gives you a serious head start on your own theming. Also of note is the use of "if (module_exists('taxonomy'))" so an error doesn't occur if taxonomy is disabled.

Remember, modules can also use preprocessor functions. You can browse the module file and examine the module preprocessor functions in the same you can examine the theme.inc functions.

Although checking these functions is usually helpful it doesn't always offer a straightforward solution, especially if you're new to Drupal. Sometimes you're presented with a programmatic Russian doll of theming and formatting functions, requiring successive digging to get the details.

Preprocessor functions are explained in detail in the Drupal.org article, Setting up variables for use in a template (preprocess functions).

Comments

Thanks for the

Thanks for the article!
Definetly reading the core file helps ... and there's no better code to learn from than drupal itself! :D

You're welcome!

You're welcome!

Helpful

Thanks for the article. I'm drupalifying my website (EggDir : General Web Direcoty) and would be needing to manually create a template for it. This is definitely gonna help.

Regards,
Sohrab Khan

I am trying to set different frontpage

for different laguage, but I cant do it
please help me

----------------------
CongTroi.VN

thanks

It was usefull to me, thanks for the time you spent for it

Helpful for designing own theme

This article is very helpful in regards to creating your own theme for drupal. I usually open up a drupal default theme and copy code into my own theme step by step. Its a very hard process since I've to copy and then check (like a trial and error) to see if i've got the desired result. I think I should try to understand how drupal is actually designed.

Great Information

This was a great help. It came in handy when trying to work with some module variables. Thanks again.

this is very helpful thank

this is very helpful thank you i also like your whole site
great work keep it up
فيديوهات

Syndicate content Syndicate content