When writing themes there's a few values we frequently need to access in our templates. I'm thinking about stuff like "is the user logged in", "is the user an admin", "what is the theme directory" (for referencing images etc)?
The good news is that all these and a few more are available in all template files. Here's a listing of what variables are available and how you can add your own.
$zebraodd or even. Perfect for generating stuff like zebra tables etc.$id$directorypath_to_theme. Example: sites/all/themes/my_theme$is_adminaccess administration pages permission set (user_access('access administration pages'))$is_frontdrupal_is_front_page()).$logged_in$userThese variables are all added using the template_preprocess function which you can find in includes/template.inc.
You don't have to stick with this list of variables but can add your own. You can do this on multiple levels: module, theme engine, theme, subtheme, ... This is a done using a preprocess function which doesn't mention the theme hook it wants to preprocess (like my_theme_preprocess(&$vars)).
You can find out how to do this in the documentation for the theme function in the Drupal API.
Comments
Davy,
Thank you for this information, which is not easy to find on the drupal.org site!
One clarification: I believe that these variables are set in
/includes/theme.inc, not/includes/template.incin Drupal 6.x.Hey there,
These links might also come in handy:
Greets y'all!
Joris_Lucius
There's more in Drupal 7:
http://api.drupal.org/api/function/_template_preprocess_default_variables/7
http://api.drupal.org/api/function/template_preprocess/7
Are those variables the same for all drupal versions (d5, d6, d7)??
Post new comment