diff options
author | Dries Buytaert <dries@buytaert.net> | 2007-04-06 13:27:23 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2007-04-06 13:27:23 +0000 |
commit | 5bbbf10ba84042b8576d67576d98922c0063c6d6 (patch) | |
tree | a2eef7bccd7d5289426b3c8edc23f52bc9f6e2ed /themes/engines/phptemplate | |
parent | 21c5b71795aec277a8b01ecea74e809a24be0229 (diff) | |
download | brdo-5bbbf10ba84042b8576d67576d98922c0063c6d6.tar.gz brdo-5bbbf10ba84042b8576d67576d98922c0063c6d6.tar.bz2 |
- Patch #130987 by merlinofchaos: added theme registry for easier themability.
Diffstat (limited to 'themes/engines/phptemplate')
-rw-r--r-- | themes/engines/phptemplate/phptemplate.engine | 406 |
1 files changed, 162 insertions, 244 deletions
diff --git a/themes/engines/phptemplate/phptemplate.engine b/themes/engines/phptemplate/phptemplate.engine index acde67a34..80ff9ea98 100644 --- a/themes/engines/phptemplate/phptemplate.engine +++ b/themes/engines/phptemplate/phptemplate.engine @@ -9,10 +9,74 @@ function phptemplate_init($template) { $file = dirname($template->filename) . '/template.php'; if (file_exists($file)) { - include_once "./$file"; + include_once "./$file"; } } +/** + * @return + * Array of template features + */ +function phptemplate_features() { + return array( + 'toggle_logo', + 'toggle_comment_user_picture', + 'toggle_favicon', + 'toggle_mission', + 'toggle_name', + 'toggle_node_user_picture', + 'toggle_search', + 'toggle_slogan' + ); +} + +/** + * Implementation of hook_themes to tell Drupal what templates the engine + * and the current theme use. The $existing argument will contain hooks + * pre-defined by Drupal so that we can use that information if + * we need to. + */ +function phptemplate_theme($existing) { + $templates = array( + 'regions' => array('function' => 'phptemplate_regions'), + 'box' => array('file' => 'box'), + 'node' => array('file' => 'node'), + 'comment' => array('file' => 'comment'), + 'block' => array('file' => 'block'), + ); + + // Check for template overrides. + $files = drupal_system_listing('\.tpl\.php$', path_to_theme(), 'name', 0); + + foreach ($files as $template => $file) { + // chop off the .tpl + $template = substr($template, 0, -4); + if (isset($existing[$template])) { + $templates[$template] = array( + 'file' => $template, + 'path' => dirname($file->filename), + ); + } + } + + // Check for function overrides. + global $theme; + foreach ($existing as $hook => $info) { + if (function_exists($theme .'_'. $hook)) { + $templates[$hook] = array( + 'function' => $theme .'_'. $hook, + ); + } + else if (function_exists('phptemplate_'. $hook)) { + $templates[$hook] = array( + 'function' => 'phptemplate_'. $hook, + ); + } + } + + return $templates; +} + function phptemplate_templates($directory = 'themes') { return drupal_system_listing('^page\.tpl\.php$', $directory, 'filename'); } @@ -34,55 +98,6 @@ function phptemplate_regions() { } /** - * Execute a template engine call. - * - * Each call to the template engine has two parts. Namely preparing - * the variables, and then doing something with them. - * - * The first step is done by all template engines / themes, the second - * step is dependent on the engine used. - * - * @param $hook - * The name of the theme function being executed. - * @param $variables - * A sequential array of variables passed to the theme function. - * @param $suggestions - * An array of suggested template files to use. If none of the files are found, the - * default $hook.tpl.php will be used. - * @return - * The HTML generated by the template system. - */ -function _phptemplate_callback($hook, $variables = array(), $suggestions = array()) { - global $theme_engine; - - $variables = array_merge($variables, _phptemplate_default_variables($hook, $variables)); - - // Allow specified variables to be overridden - $variables_function = '_'. $theme_engine .'_variables'; - if (function_exists($variables_function)) { - $variables = array_merge($variables, call_user_func($variables_function, $hook, $variables)); - } - - if (isset($variables['template_files'])) { - $suggestions = array_merge($suggestions, $variables['template_files']); - } - - if (isset($variables['template_file'])) { - $suggestions[] = $variables['template_file']; - } - - $hook_function = '_'. $theme_engine .'_'. $hook; - $default_function = '_'. $theme_engine .'_default'; - if (function_exists($hook_function)) { - return call_user_func($hook_function, $variables, $suggestions); - } - elseif (function_exists($default_function)) { - return call_user_func($default_function, $hook, $variables, $suggestions); - } - -} - -/** * Adds additional helper variables to all templates. * * Counts how many times certain hooks have been called. Sidebar left / right are special cases. @@ -92,66 +107,29 @@ function _phptemplate_callback($hook, $variables = array(), $suggestions = array * @param $variables * A sequential array of variables passed to the theme function. */ -function _phptemplate_default_variables($hook, $variables) { +function phptemplate_engine_variables(&$variables, $hook) { global $theme, $sidebar_indicator; static $count = array(); + // Create variables so anything which is themed can be zebra striped automatically. $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1; $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even'; $variables['id'] = $count[$hook]++; - if ($hook == 'block') { - $count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1; - $variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even'; - $variables['block_id'] = $count['block_counter'][$sidebar_indicator]++; - } - elseif ($hook == 'page') { - $regions = system_region_list($theme); - // Load all region content assigned via blocks. - foreach (array_keys($regions) as $region) { - // Skip blocks in this region that have already been loaded. - // This pre-loading is necessary because phptemplate uses variable names different from - // the region names, e.g., 'sidebar_left' instead of 'left'. - if (!in_array($region, array('left', 'right', 'footer'))) { - isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region); - } - } - } // Tell all templates where they are located. $variables['directory'] = path_to_theme(); $variables['is_front'] = drupal_is_front_page(); - - return $variables; } /** - * @return - * Array of template features + * Prepare the variables passed to the page.tpl.php template Uses the arg() + * function to generate a series of page template files suggestions based on + * the current path. */ -function phptemplate_features() { - return array( - 'toggle_logo', - 'toggle_comment_user_picture', - 'toggle_favicon', - 'toggle_mission', - 'toggle_name', - 'toggle_node_user_picture', - 'toggle_search', - 'toggle_slogan' - ); -} - -/** - * Prepare the values passed to the theme_page function to be passed - * into a pluggable template engine. Uses the arg() function to - * generate a series of page template files suggestions based on the - * current path. If none are found, the default page.tpl.php is used. - */ -function phptemplate_page($content, $show_blocks = TRUE) { - +function phptemplate_engine_variables_page(&$variables) { /* Set title and breadcrumb to declared values */ if (drupal_is_front_page()) { - $mission = filter_xss_admin(theme_get_setting('mission')); + $variables['mission'] = filter_xss_admin(theme_get_setting('mission')); } /* Add favicon */ @@ -159,26 +137,44 @@ function phptemplate_page($content, $show_blocks = TRUE) { drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />'); } - // Populate sidebars + /** + * Populate sidebars. + */ + $variables['sidebar_left'] = NULL; + $variables['sidebar_right'] = NULL; $layout = 'none'; - if ($show_blocks) { + if ($variables['show_blocks']) { global $sidebar_indicator; /** * Sidebar_indicator tells the block counting code to count sidebars separately. */ $sidebar_indicator = 'left'; - $sidebar_left = theme('blocks', 'left'); - if ($sidebar_left != '') { + $variables['sidebar_left'] = theme('blocks', 'left'); + if ($variables['sidebar_left'] != '') { $layout = 'left'; } $sidebar_indicator = 'right'; - $sidebar_right = theme('blocks', 'right'); - if ($sidebar_right != '') { - $layout = ($layout == 'left') ? 'both' : 'right'; + $variables['sidebar_right'] = theme('blocks', 'right'); + if ($variables['sidebar_right'] != '') { + $variables['layout'] = ($layout == 'left') ? 'both' : 'right'; } $sidebar_indicator = NULL; } + $variables['layout'] = $layout; + + global $theme; + // Populate the rest of the regions. + $regions = system_region_list($theme); + // Load all region content assigned via blocks. + foreach (array_keys($regions) as $region) { + // Skip blocks in this region that have already been loaded. + // This pre-loading is necessary because phptemplate uses variable names different from + // the region names, e.g., 'sidebar_left' instead of 'left'. + if (!in_array($region, array('left', 'right', 'footer'))) { + isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region); + } + } // Construct page title if (drupal_get_title()) { @@ -190,35 +186,28 @@ function phptemplate_page($content, $show_blocks = TRUE) { $head_title[] = variable_get('site_slogan', ''); } } - - $variables = array( - 'base_path' => base_path(), - 'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()), - 'closure' => theme('closure'), - 'content' => $content, - 'feed_icons' => drupal_get_feeds(), - '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['language'], - 'layout' => isset($layout) ? $layout : NULL, - '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') ? drupal_get_form('search_theme_form') : ''), - 'secondary_links' => menu_secondary_links(), - 'sidebar_left' => !empty($sidebar_left) ? $sidebar_left : '', - 'sidebar_right' => !empty($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', '') : ''), - 'css' => drupal_add_css(), - 'styles' => drupal_get_css(), - 'scripts' => drupal_get_js(), - 'tabs' => theme('menu_local_tasks'), - 'title' => drupal_get_title() - ); + $variables['head_title'] = implode(' | ', $head_title); + $variables['base_path'] = base_path(); + $variables['breadcrumb'] = theme('breadcrumb', drupal_get_breadcrumb()); + $variables['closure'] = theme('closure'); + $variables['feed_icons'] = drupal_get_feeds(); + $variables['footer_message'] = filter_xss_admin(variable_get('site_footer', FALSE)) . "\n" . theme('blocks', 'footer'); + $variables['head'] = drupal_get_html_head(); + $variables['help'] = theme('help'); + $variables['language'] = $GLOBALS['language']; + $variables['logo'] = theme_get_setting('logo'); + $variables['messages'] = theme('status_messages'); + $variables['mission'] = isset($mission) ? $mission : ''; + $variables['primary_links'] = menu_primary_links(); + $variables['search_box'] = (theme_get_setting('toggle_search') ? drupal_get_form('search_theme_form') : ''); + $variables['secondary_links'] = menu_secondary_links(); + $variables['site_name'] = (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''); + $variables['site_slogan'] = (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''); + $variables['css'] = drupal_add_css(); + $variables['styles'] = drupal_get_css(); + $variables['scripts'] = drupal_get_js(); + $variables['tabs'] = theme('menu_local_tasks'); + $variables['title'] = drupal_get_title(); if ((arg(0) == 'node') && is_numeric(arg(1))) { $variables['node'] = node_load(arg(1)); @@ -236,7 +225,7 @@ function phptemplate_page($content, $show_blocks = TRUE) { // page.tpl.php $i = 0; $suggestion = 'page'; - $suggestions = array($suggestion); + $suggestions = array(); while ($arg = arg($i++)) { $suggestions[] = $suggestion . '-' . $arg; if (!is_numeric($arg)) { @@ -247,43 +236,40 @@ function phptemplate_page($content, $show_blocks = TRUE) { $suggestions[] = 'page-front'; } - return _phptemplate_callback('page', $variables, $suggestions); + if ($suggestions) { + $variables['template_files'] = $suggestions; + } } /* * Prepare the values passed to the theme_node function to be passed - * into a pluggable template engine. + * into standard template files. */ -function phptemplate_node($node, $teaser = 0, $page = 0) { +function phptemplate_engine_variables_node(&$variables) { + $node = $variables['node']; if (module_exists('taxonomy')) { - $taxonomy = taxonomy_link('taxonomy terms', $node); + $variables['taxonomy'] = taxonomy_link('taxonomy terms', $node); } else { - $taxonomy = array(); + $variables['taxonomy'] = array(); } - if ($teaser && $node->teaser) { - $content = $node->teaser; + + if ($variables['teaser'] && $node->teaser) { + $variables['content'] = $node->teaser; } elseif (isset($node->body)) { - $content = $node->body; + $variables['content'] = $node->body; } else { - $content = ''; + $variables['content'] = ''; } - $variables = array( - 'content' => $content, - 'date' => format_date($node->created), - 'links' => !empty($node->links) ? theme('links', $node->links, array('class' => 'links inline')) : '', - 'name' => theme('username', $node), - 'node' => $node, // we pass the actual node to allow more customization - 'node_url' => url('node/'. $node->nid), - 'page' => $page, - 'taxonomy' => $taxonomy, - 'teaser' => $teaser, - 'terms' => theme('links', $taxonomy, array('class' => 'links inline')), - 'title' => check_plain($node->title) - ); + $variables['date'] = format_date($node->created); + $variables['links'] = !empty($node->links) ? theme('links', $node->links, array('class' => 'links inline')) : ''; + $variables['name'] = theme('username', $node); + $variables['node_url'] = url('node/'. $node->nid); + $variables['terms'] = theme('links', $variables['taxonomy'], array('class' => 'links inline')); + $variables['title'] = check_plain($node->title); // Flatten the node object's member fields. $variables = array_merge((array)$node, $variables); @@ -298,28 +284,28 @@ function phptemplate_node($node, $teaser = 0, $page = 0) { $variables['picture'] = ''; } - return _phptemplate_callback('node', $variables, array('node-' . $node->type)); + $variables['template_files'][] = 'node-'. $node->type; } /** * Prepare the values passed to the theme_comment function to be passed * into a pluggable template engine. */ -function phptemplate_comment($comment, $links = 0) { - return _phptemplate_callback('comment', array( - 'author' => theme('username', $comment), - 'comment' => $comment, - 'content' => $comment->comment, - 'date' => format_date($comment->timestamp), - 'links' => isset($links) ? theme('links', $links) : '', - 'new' => $comment->new ? t('new') : '', - 'signature' => $comment->signature, - 'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '', - 'submitted' => t('Submitted by !a on @b.', +// function phptemplate_comment($comment, $links = 0) { +function phptemplate_engine_variables_comment(&$variables) { + $comment = $variables['comment']; + $variables['author'] = theme('username', $comment); + $variables['comment'] = $comment; + $variables['content'] = $comment->comment; + $variables['date'] = format_date($comment->timestamp); + $variables['links'] = isset($variables['links']) ? theme('links', $variables['links']) : ''; + $variables['new'] = $comment->new ? t('new') : ''; + $variables['picture'] = theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : ''; + $variables['signature'] = $comment->signature; + $variables['submitted'] = t('Submitted by !a on @b.', array('!a' => theme('username', $comment), - '@b' => format_date($comment->timestamp))), - 'title' => l($comment->subject, $_GET['q'], array('fragment' => "comment-$comment->cid")) - )); + '@b' => format_date($comment->timestamp))); + $variables['title'] = l($comment->subject, $_GET['q'], array('fragment' => "comment-$comment->cid")); } /** @@ -328,82 +314,14 @@ function phptemplate_comment($comment, $links = 0) { * series of template file suggestions. If none are found, the default * block.tpl.php is used. */ -function phptemplate_block($block) { - $suggestions[] = 'block'; - $suggestions[] = 'block-' . $block->region; - $suggestions[] = 'block-' . $block->module; - $suggestions[] = 'block-' . $block->module . '-' . $block->delta; +function phptemplate_engine_variables_block(&$variables) { + global $sidebar_indicator; + $count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1; - return _phptemplate_callback('block', array('block' => $block), $suggestions); -} + $variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even'; -/** - * Prepare the values passed to the theme_box function to be passed - * into a pluggable template engine. - */ -function phptemplate_box($title, $content, $region = 'main') { - return _phptemplate_callback('box', array( - 'content' => $content, - 'region' => $region, - 'title' => $title - )); + $variables['block_id'] = $count['block_counter'][$sidebar_indicator]++; + $variables['template_files'][] = 'block-' . $variables['block']->region; + $variables['template_files'][] = 'block-' . $variables['block']->module; + $variables['template_files'][] = 'block-' . $variables['block']->module .'-'. $variables['block']->delta; } - -/** - * Default callback for PHPTemplate. - * - * Load a template file, and pass the variable array to it. - * If the suggested file is not found, PHPTemplate will attempt to use - * a $hook.tpl.php file in the template directory, and failing that a - * $hook.tpl.php in the PHPTemplate directory. - * - * @param $hook - * The name of the theme function being executed. - * @param $variables - * A sequential array of variables passed to the theme function. - * @param $suggestions - * An array of suggested template files to use. - */ -function _phptemplate_default($hook, $variables, $suggestions = array(), $extension = '.tpl.php') { - global $theme_engine; - - // Loop through any suggestions in FIFO order. - $suggestions = array_reverse($suggestions); - foreach ($suggestions as $suggestion) { - if (!empty($suggestion) && file_exists(path_to_theme() .'/'. $suggestion . $extension)) { - $file = path_to_theme() .'/'. $suggestion . $extension; - break; - } - } - - if (!isset($file)) { - if (file_exists(path_to_theme() ."/$hook$extension")) { - $file = path_to_theme() ."/$hook$extension"; - } - else { - if (in_array($hook, array('node', 'block', 'box', 'comment'))) { - $file = path_to_engine() .'/'. $hook . $extension; - } - else { - $variables['hook'] = $hook; - watchdog('error', t('%engine.engine was instructed to override the %name theme function, but no valid template file was found.', array('%engine' => $theme_engine, '%name' => $hook))); - $file = path_to_engine() .'/default'. $extension; - } - } - } - - if (isset($file)) { - return call_user_func('_'. $theme_engine .'_render', $file, $variables); - } -} - -function _phptemplate_render($file, $variables) { - extract($variables, EXTR_SKIP); // Extract the variables to a local namespace - ob_start(); // Start output buffering - include "./$file"; // Include the file - $contents = ob_get_contents(); // Get the contents of the buffer - ob_end_clean(); // End buffering and discard - return $contents; // Return the contents -} - -?> |