--- smarty.engine.old 2006-01-10 09:12:51.000000000 +0000 +++ smarty.engine 2007-03-31 20:17:16.000000000 +0100 @@ -23,7 +23,7 @@ */ function smarty_templates($directory = 'themes') { _test_permissions(); - return system_listing('^page\.tpl$', $directory, 'filename'); + return drupal_system_listing('^page\.tpl$', $directory, 'filename'); } /** @@ -60,25 +60,32 @@ * @return * The HTML generated by the template system. */ -function _smarty_callback($hook, $variables = array(), $file = NULL) { +function _smarty_callback($hook, $variables = array(), $suggestions = array()) { + global $theme_engine; $variables = array_merge($variables, _smarty_default_variables($hook, $variables)); // Allow specified variables to be overridden - if (function_exists('_smarty_variables')) { - $variables = array_merge($variables, _smarty_variables($hook, $variables)); + $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_file'])) { - $file = $variables['template_file']; + if (isset($variables['template_files'])) { + $suggestions = array_merge($suggestions, $variables['template_files']); } - // Call associated theme function or pass to _smarty_default - if (function_exists('_smarty_' . $hook)) { - return call_user_func('_smarty_' . $hook, $variables, $file); + if (isset($variables['template_file'])) { + $suggestions[] = $variables['template_file']; } - elseif (function_exists('_smarty_default')) { - return call_user_func('_smarty_default', $hook, $variables, $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); } } @@ -94,13 +101,13 @@ * A sequential array of variables passed to the theme function. */ function _smarty_default_variables($hook, $variables) { - global $theme; + global $theme, $sidebar_indicator; static $count = array(); + $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1; $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even'; $variables['id'] = $count[$hook]++; - global $sidebar_indicator; 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'; @@ -120,10 +127,7 @@ } // Tell all templates where they are located. $variables['directory'] = path_to_theme(); - - if (drupal_get_path_alias($_GET['q']) == variable_get('site_frontpage', 'node')) { - $variables['is_front'] = true; - } + $variables['is_front'] = drupal_is_front_page(); return $variables; } @@ -195,10 +199,12 @@ } $variables = array( + 'base_path' => base_path(), 'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()), 'closure' => theme('closure'), 'content' => '' . $content . '', - 'footer_message' => variable_get('site_footer', FALSE) . "\n" . theme('blocks', 'footer'), + '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'), @@ -215,7 +221,8 @@ '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(), + 'styles' => drupal_get_css(), + 'scripts' => drupal_get_js(), 'tabs' => theme('menu_local_tasks'), 'title' => drupal_get_title() ); @@ -223,8 +230,31 @@ if ((arg(0) == 'node') && is_numeric(arg(1))) { $variables['node'] = node_load(arg(1)); } + + // Build a list of suggested template files in order of specificity. One + // suggestion is made for every element of the current path, though + // numeric elements are not carried to subsequent suggestions. For example, + // http://www.example.com/node/1/edit would result in the following + // suggestions: + // + // page-node-edit.tpl + // page-node-1.tpl + // page-node.tpl + // page.tpl + $i = 0; + $suggestion = 'page'; + $suggestions = array($suggestion); + while ($arg = arg($i++)) { + $suggestions[] = $suggestion . '-' . $arg; + if (!is_numeric($arg)) { + $suggestion .= '-' . $arg; + } + } + if (drupal_is_front_page()) { + $suggestions[] = 'page-front'; + } - return _smarty_callback('page', $variables); + return _smarty_callback('page', $variables, $suggestions); } /** @@ -232,7 +262,7 @@ * into a pluggable template engine. */ function smarty_node($node, $teaser = 0, $page = 0) { - if (module_exist('taxonomy')) { + if (module_exists('taxonomy')) { $taxonomy = taxonomy_link('taxonomy terms', $node); } else { @@ -290,7 +320,11 @@ * into a pluggable template engine. */ function smarty_block($block) { - return _smarty_callback('block', array('block' => $block)); + $suggestions[] = 'block'; + $suggestions[] = 'block-' . $block->region; + $suggestions[] = 'block-' . $block->module; + $suggestions[] = 'block-' . $block->module . '-' . $block->delta; + return _smarty_callback('block', array('block' => $block), $suggestions); } /** @@ -317,27 +351,36 @@ * The name of the theme function being executed. * @param $variables * A sequential array of variables passed to the theme function. - * @param $file - * A suggested template file to use. + * @param $suggestions + * An array of suggested template files to use. */ -function _smarty_default($hook, $variables, $file = NULL) { +function _smarty_default($hook, $variables, $suggestions = array(), $extension = '.tpl') { global $smarty; + global $theme_engine; - if (!empty($file) && file_exists(path_to_theme() . "/$file.tpl")) { - $file = path_to_theme() . "/$file.tpl"; + // Loop through any suggestions in FIFO order. + if (is_array($suggestions) && count($suggestions) > 0) { + $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; + } + } } - else { - if (file_exists(path_to_theme() . "/$hook.tpl")) { - $file = path_to_theme() . "/$hook.tpl"; + + 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 = "themes/engines/smarty/$hook.tpl"; + $file = path_to_engine() .'/'. $hook . $extension; } else { $variables['hook'] = $hook; - watchdog('error', t('Smarty Theme Engine was instructed to override the %name theme function, but no valid template file was found.', array('%name' => theme('placeholder', $hook)))); - $file = 'themes/engines/smarty/default.tpl'; + 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; } } }