[ Index ] |
|
Code source de Drupal 5.3 |
1 <?php 2 // $Id: phptemplate.engine,v 1.54.2.2 2007/05/31 06:21:32 drumm Exp $ 3 4 /** 5 * @file 6 * Handles integration of templates written in pure php with the Drupal theme system. 7 */ 8 9 function phptemplate_init($template) { 10 $file = dirname($template->filename) . '/template.php'; 11 if (file_exists($file)) { 12 include_once "./$file"; 13 } 14 } 15 16 function phptemplate_templates($directory = 'themes') { 17 return drupal_system_listing('^page\.tpl\.php$', $directory, 'filename'); 18 } 19 20 /** 21 * Declare the available regions implemented by this engine. 22 * 23 * @return 24 * An array of regions. The first array element will be used as the default region for themes. 25 */ 26 function phptemplate_regions() { 27 return array( 28 'left' => t('left sidebar'), 29 'right' => t('right sidebar'), 30 'content' => t('content'), 31 'header' => t('header'), 32 'footer' => t('footer') 33 ); 34 } 35 36 /** 37 * Execute a template engine call. 38 * 39 * Each call to the template engine has two parts. Namely preparing 40 * the variables, and then doing something with them. 41 * 42 * The first step is done by all template engines / themes, the second 43 * step is dependent on the engine used. 44 * 45 * @param $hook 46 * The name of the theme function being executed. 47 * @param $variables 48 * A sequential array of variables passed to the theme function. 49 * @param $suggestions 50 * An array of suggested template files to use. If none of the files are found, the 51 * default $hook.tpl.php will be used. 52 * @return 53 * The HTML generated by the template system. 54 */ 55 function _phptemplate_callback($hook, $variables = array(), $suggestions = array()) { 56 global $theme_engine; 57 58 $variables = array_merge($variables, _phptemplate_default_variables($hook, $variables)); 59 60 // Allow specified variables to be overridden 61 $variables_function = '_'. $theme_engine .'_variables'; 62 if (function_exists($variables_function)) { 63 $variables = array_merge($variables, call_user_func($variables_function, $hook, $variables)); 64 } 65 66 if (isset($variables['template_files'])) { 67 $suggestions = array_merge($suggestions, $variables['template_files']); 68 } 69 70 if (isset($variables['template_file'])) { 71 $suggestions[] = $variables['template_file']; 72 } 73 74 $hook_function = '_'. $theme_engine .'_'. $hook; 75 $default_function = '_'. $theme_engine .'_default'; 76 if (function_exists($hook_function)) { 77 return call_user_func($hook_function, $variables, $suggestions); 78 } 79 elseif (function_exists($default_function)) { 80 return call_user_func($default_function, $hook, $variables, $suggestions); 81 } 82 83 } 84 85 /** 86 * Adds additional helper variables to all templates. 87 * 88 * Counts how many times certain hooks have been called. Sidebar left / right are special cases. 89 * 90 * @param $hook 91 * The name of the theme function being executed. 92 * @param $variables 93 * A sequential array of variables passed to the theme function. 94 */ 95 function _phptemplate_default_variables($hook, $variables) { 96 global $theme, $sidebar_indicator; 97 static $count = array(); 98 99 $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1; 100 $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even'; 101 $variables['id'] = $count[$hook]++; 102 103 if ($hook == 'block') { 104 $count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1; 105 $variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even'; 106 $variables['block_id'] = $count['block_counter'][$sidebar_indicator]++; 107 } 108 elseif ($hook == 'page') { 109 $regions = system_region_list($theme); 110 // Load all region content assigned via blocks. 111 foreach (array_keys($regions) as $region) { 112 // Skip blocks in this region that have already been loaded. 113 // This pre-loading is necessary because phptemplate uses variable names different from 114 // the region names, e.g., 'sidebar_left' instead of 'left'. 115 if (!in_array($region, array('left', 'right', 'footer'))) { 116 isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region); 117 } 118 } 119 } 120 // Tell all templates where they are located. 121 $variables['directory'] = path_to_theme(); 122 $variables['is_front'] = drupal_is_front_page(); 123 124 return $variables; 125 } 126 127 /** 128 * @return 129 * Array of template features 130 */ 131 function phptemplate_features() { 132 return array( 133 'toggle_logo', 134 'toggle_comment_user_picture', 135 'toggle_favicon', 136 'toggle_mission', 137 'toggle_name', 138 'toggle_node_user_picture', 139 'toggle_search', 140 'toggle_slogan' 141 ); 142 } 143 144 /** 145 * Prepare the values passed to the theme_page function to be passed 146 * into a pluggable template engine. Uses the arg() function to 147 * generate a series of page template files suggestions based on the 148 * current path. If none are found, the default page.tpl.php is used. 149 */ 150 function phptemplate_page($content, $show_blocks = TRUE) { 151 152 /* Set title and breadcrumb to declared values */ 153 if (drupal_is_front_page()) { 154 $mission = filter_xss_admin(theme_get_setting('mission')); 155 } 156 157 /* Add favicon */ 158 if (theme_get_setting('toggle_favicon')) { 159 drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />'); 160 } 161 162 // Populate sidebars 163 $layout = 'none'; 164 if ($show_blocks) { 165 global $sidebar_indicator; 166 /** 167 * Sidebar_indicator tells the block counting code to count sidebars separately. 168 */ 169 $sidebar_indicator = 'left'; 170 $sidebar_left = theme('blocks', 'left'); 171 if ($sidebar_left != '') { 172 $layout = 'left'; 173 } 174 175 $sidebar_indicator = 'right'; 176 $sidebar_right = theme('blocks', 'right'); 177 if ($sidebar_right != '') { 178 $layout = ($layout == 'left') ? 'both' : 'right'; 179 } 180 $sidebar_indicator = NULL; 181 } 182 183 // Construct page title 184 if (drupal_get_title()) { 185 $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'Drupal')); 186 } 187 else { 188 $head_title = array(variable_get('site_name', 'Drupal')); 189 if (variable_get('site_slogan', '')) { 190 $head_title[] = variable_get('site_slogan', ''); 191 } 192 } 193 194 $variables = array( 195 'base_path' => base_path(), 196 'breadcrumb' => theme('breadcrumb', drupal_get_breadcrumb()), 197 'closure' => theme('closure'), 198 'content' => $content, 199 'feed_icons' => drupal_get_feeds(), 200 'footer_message' => filter_xss_admin(variable_get('site_footer', FALSE)) . "\n" . theme('blocks', 'footer'), 201 'head' => drupal_get_html_head(), 202 'head_title' => implode(' | ', $head_title), 203 'help' => theme('help'), 204 'language' => $GLOBALS['locale'], 205 'layout' => $layout, 206 'logo' => theme_get_setting('logo'), 207 'messages' => theme('status_messages'), 208 'mission' => isset($mission) ? $mission : '', 209 'primary_links' => menu_primary_links(), 210 'search_box' => (theme_get_setting('toggle_search') ? drupal_get_form('search_theme_form') : ''), 211 'secondary_links' => menu_secondary_links(), 212 'sidebar_left' => $sidebar_left, 213 'sidebar_right' => $sidebar_right, 214 'site_name' => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''), 215 'site_slogan' => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''), 216 'css' => drupal_add_css(), 217 'styles' => drupal_get_css(), 218 'scripts' => drupal_get_js(), 219 'tabs' => theme('menu_local_tasks'), 220 'title' => drupal_get_title() 221 ); 222 223 if ((arg(0) == 'node') && is_numeric(arg(1))) { 224 $variables['node'] = node_load(arg(1)); 225 } 226 227 // Build a list of suggested template files in order of specificity. One 228 // suggestion is made for every element of the current path, though 229 // numeric elements are not carried to subsequent suggestions. For example, 230 // http://www.example.com/node/1/edit would result in the following 231 // suggestions: 232 // 233 // page-node-edit.tpl.php 234 // page-node-1.tpl.php 235 // page-node.tpl.php 236 // page.tpl.php 237 $i = 0; 238 $suggestion = 'page'; 239 $suggestions = array($suggestion); 240 while ($arg = arg($i++)) { 241 $suggestions[] = $suggestion . '-' . $arg; 242 if (!is_numeric($arg)) { 243 $suggestion .= '-' . $arg; 244 } 245 } 246 if (drupal_is_front_page()) { 247 $suggestions[] = 'page-front'; 248 } 249 250 return _phptemplate_callback('page', $variables, $suggestions); 251 } 252 253 /** 254 * Prepare the values passed to the theme_node function to be passed 255 * into a pluggable template engine. 256 */ 257 function phptemplate_node($node, $teaser = 0, $page = 0) { 258 if (module_exists('taxonomy')) { 259 $taxonomy = taxonomy_link('taxonomy terms', $node); 260 } 261 else { 262 $taxonomy = array(); 263 } 264 265 $variables = array( 266 'content' => ($teaser && $node->teaser) ? $node->teaser : $node->body, 267 'date' => format_date($node->created), 268 'links' => $node->links ? theme('links', $node->links, array('class' => 'links inline')) : '', 269 'name' => theme('username', $node), 270 'node' => $node, // we pass the actual node to allow more customization 271 'node_url' => url('node/'. $node->nid), 272 'page' => $page, 273 'taxonomy' => $taxonomy, 274 'teaser' => $teaser, 275 'terms' => theme('links', $taxonomy, array('class' => 'links inline')), 276 'title' => check_plain($node->title) 277 ); 278 279 // Flatten the node object's member fields. 280 $variables = array_merge((array)$node, $variables); 281 282 // Display info only on certain node types. 283 if (theme_get_setting('toggle_node_info_' . $node->type)) { 284 $variables['submitted'] = t('Submitted by !a on @b.', array('!a' => theme('username', $node), '@b' => format_date($node->created))); 285 $variables['picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : ''; 286 } 287 else { 288 $variables['submitted'] = ''; 289 $variables['picture'] = ''; 290 } 291 292 return _phptemplate_callback('node', $variables, array('node-' . $node->type)); 293 } 294 295 /** 296 * Prepare the values passed to the theme_comment function to be passed 297 * into a pluggable template engine. 298 */ 299 function phptemplate_comment($comment, $links = 0) { 300 return _phptemplate_callback('comment', array( 301 'author' => theme('username', $comment), 302 'comment' => $comment, 303 'content' => $comment->comment, 304 'date' => format_date($comment->timestamp), 305 'links' => isset($links) ? theme('links', $links) : '', 306 'new' => $comment->new ? t('new') : '', 307 'picture' => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '', 308 'submitted' => t('Submitted by !a on @b.', 309 array('!a' => theme('username', $comment), 310 '@b' => format_date($comment->timestamp))), 311 'title' => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid") 312 )); 313 } 314 315 /** 316 * Prepare the values passed to the theme_block function to be passed 317 * into a pluggable template engine. Uses block properties to generate a 318 * series of template file suggestions. If none are found, the default 319 * block.tpl.php is used. 320 */ 321 function phptemplate_block($block) { 322 $suggestions[] = 'block'; 323 $suggestions[] = 'block-' . $block->region; 324 $suggestions[] = 'block-' . $block->module; 325 $suggestions[] = 'block-' . $block->module . '-' . $block->delta; 326 327 return _phptemplate_callback('block', array('block' => $block), $suggestions); 328 } 329 330 /** 331 * Prepare the values passed to the theme_box function to be passed 332 * into a pluggable template engine. 333 */ 334 function phptemplate_box($title, $content, $region = 'main') { 335 return _phptemplate_callback('box', array( 336 'content' => $content, 337 'region' => $region, 338 'title' => $title 339 )); 340 } 341 342 /** 343 * Default callback for PHPTemplate. 344 * 345 * Load a template file, and pass the variable array to it. 346 * If the suggested file is not found, PHPTemplate will attempt to use 347 * a $hook.tpl.php file in the template directory, and failing that a 348 * $hook.tpl.php in the PHPTemplate directory. 349 * 350 * @param $hook 351 * The name of the theme function being executed. 352 * @param $variables 353 * A sequential array of variables passed to the theme function. 354 * @param $suggestions 355 * An array of suggested template files to use. 356 */ 357 function _phptemplate_default($hook, $variables, $suggestions = array(), $extension = '.tpl.php') { 358 global $theme_engine; 359 360 // Loop through any suggestions in FIFO order. 361 $suggestions = array_reverse($suggestions); 362 foreach ($suggestions as $suggestion) { 363 if (!empty($suggestion) && file_exists(path_to_theme() .'/'. $suggestion . $extension)) { 364 $file = path_to_theme() .'/'. $suggestion . $extension; 365 break; 366 } 367 } 368 369 if (!isset($file)) { 370 if (file_exists(path_to_theme() ."/$hook$extension")) { 371 $file = path_to_theme() ."/$hook$extension"; 372 } 373 else { 374 if (in_array($hook, array('node', 'block', 'box', 'comment'))) { 375 $file = path_to_engine() .'/'. $hook . $extension; 376 } 377 else { 378 $variables['hook'] = $hook; 379 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))); 380 $file = path_to_engine() .'/default'. $extension; 381 } 382 } 383 } 384 385 if (isset($file)) { 386 return call_user_func('_'. $theme_engine .'_render', $file, $variables); 387 } 388 } 389 390 function _phptemplate_render($file, $variables) { 391 extract($variables, EXTR_SKIP); // Extract the variables to a local namespace 392 ob_start(); // Start output buffering 393 include "./$file"; // Include the file 394 $contents = ob_get_contents(); // Get the contents of the buffer 395 ob_end_clean(); // End buffering and discard 396 return $contents; // Return the contents 397 } 398 399 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Fri Nov 30 16:20:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |