Template Theme Variables

[Update 2007-Apr-29: Please also see the article Theme Variable Total Visibility]

Have you ever wanted to know what variables the phptemplate engine made to the node or page templates? "Use the source, Luke" (Couldn't resist). The way of finding the definitive answer was discussed in Drupal Dojo today.

The phptemplate engine lives in the directory: themes/engines/phptemplate. There you'll find the phptemplate.engine file which contains the functions: phptemplate_page() and phptemplate_node(). Among other things, these functions construct the array used to create the variables. For example, in the phptemplate_page() you'll find:

  $variables = array(
    'base_path'           => base_path(),
    'breadcrumb'          => theme('breadcrumb', drupal_get_breadcrumb()),
    'closure'             => theme('closure'),
    'content'             => '<!-- begin content -->' 
                               . $content . '&lt!-- end content -->',
    'footer_message'      => filter_xss_admin(variable_get('site_footer', FALSE)) 
                               . "\n" . theme('blocks', 'footer'),
    'head'                => drupal_get_html_head(),
    'head_title'          => implode(' | ', $head_title),
    'help'                => theme('help'),
    'language'            => $GLOBALS['locale'],
    'layout'              => $layout,
    'logo'                => theme_get_setting('logo'),
    'messages'            => theme('status_messages'),
    'mission'             => isset($mission) ? $mission : '',
    'primary_links'       => menu_primary_links(),
    'search_box'          => (theme_get_setting('toggle_search') ? search_box() : ''),
    'secondary_links'     => menu_secondary_links(),
    'sidebar_left'        => $sidebar_left,
    'sidebar_right'       => $sidebar_right,
    'site_name'           => (theme_get_setting('toggle_name') ? 
                                variable_get('site_name', 'Drupal') : ''),
    'site_slogan'         => (theme_get_setting('toggle_slogan') ? 
                                variable_get('site_slogan', '') : ''),
    'styles'              => theme_get_styles(),
    'tabs'                => theme('menu_local_tasks'),
    'title'               => drupal_get_title()
  );

  if ((arg(0) == 'node') && is_numeric(arg(1))) {
    $variables['node'] = node_load(arg(1));
  }

The associative array key becomes the variable name. e.g. $title, $base_path, $mission, $node (if it's a node).

This is true for Drupal 4.7 and Drupal 5.0.