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
thanks
It was usefull to me, thanks for the time you spent for it