phptemplate.engine 11.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php
// $Id$

/**
 * @file
 * Handles integration of templates written in pure php with the Drupal theme system.
 */

function phptemplate_init($template) {
  $file = dirname($template->filename) . '/template.php';
  if (file_exists($file)) {
12
   include_once "./$file";
13 14 15 16 17 18 19
  }
}

function phptemplate_templates($directory = 'themes') {
  return system_listing('^page\.tpl\.php$', $directory, 'filename');
}

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/**
 * Declare the available regions implemented by this engine.
 *
 * @return
 *  An array of regions.  The first array element will be used as the default region for themes.
 */
function phptemplate_regions() {
  return array(
       'left' => t('left sidebar'),
       'right' => t('right sidebar'),
       'content' => t('content'),
       'header' => t('header'),
       'footer' => t('footer')
  );
}

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/**
 * 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 $file
 *   A suggested template file to use. If the file is not found, the default $hook.tpl.php will be used.
 * @return
 *  The HTML generated by the template system.
 */
54
function _phptemplate_callback($hook, $variables = array(), $file = NULL) {
55 56 57 58 59 60 61 62

  $variables = array_merge($variables, _phptemplate_default_variables($hook, $variables));

  // Allow specified variables to be overridden
  if (function_exists('_phptemplate_variables')) {
    $variables = array_merge($variables, _phptemplate_variables($hook, $variables));
  }

63
  if (isset($variables['template_file'])) {
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    $file = $variables['template_file'];
  }

  if (function_exists('_phptemplate_' . $hook)) {
    return call_user_func('_phptemplate_' . $hook, $variables, $file);
  }
  elseif (function_exists('_phptemplate_default')) {
    return call_user_func('_phptemplate_default', $hook, $variables, $file);
  }

}

/**
 * Adds additional helper variables to all templates.
 *
 * Counts how many times certain hooks have been called. Sidebar left / right are special cases.
 *
 * @param $hook
 *   The name of the theme function being executed.
 * @param $variables
 *   A sequential array of variables passed to the theme function.
 */
function _phptemplate_default_variables($hook, $variables) {
87
  global $theme;
88
  static $count = array();
89
  $count[$hook] = isset($count[$hook]) && is_int($count[$hook]) ? $count[$hook] : 1;
90 91 92 93 94
  $variables['zebra'] = ($count[$hook] % 2) ? 'odd' : 'even';
  $variables['id'] = $count[$hook]++;

  global $sidebar_indicator;
  if ($hook == 'block') {
95
    $count['block_counter'][$sidebar_indicator] = isset($count['block_counter'][$sidebar_indicator]) && is_int($count['block_counter'][$sidebar_indicator]) ? $count['block_counter'][$sidebar_indicator] : 1;
96 97 98
    $variables['block_zebra'] = ($count['block_counter'][$sidebar_indicator] % 2) ? 'odd' : 'even';
    $variables['block_id'] = $count['block_counter'][$sidebar_indicator]++;
  }
99 100 101 102 103 104 105 106
  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'))) {
107
        isset($variables[$region]) ? $variables[$region] .= theme('blocks', $region) : $variables[$region] = theme('blocks', $region);
108 109 110
      }
    }
  }
111 112 113 114 115 116 117 118 119 120
  // 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;
  }

  return $variables;
}

121 122 123 124
/**
 * @return
 *  Array of template features
 */
125 126
function phptemplate_features() {
  return array(
127 128 129 130 131 132 133 134 135
    'logo',
    'toggle_comment_user_picture',
    'toggle_favicon',
    'toggle_mission',
    'toggle_name',
    'toggle_node_user_picture',
    'toggle_search',
    'toggle_slogan'
  );
136 137 138 139 140 141 142
}

/**
 * Prepare the values passed to the theme_page function to be passed
 * into a pluggable template engine.
 */
function phptemplate_page($content) {
143
  global $base_path;
144 145 146 147 148 149 150
  /* Set title and breadcrumb to declared values */

  if ($_GET['q'] == variable_get('site_frontpage', 'node')) {
    $mission = theme_get_setting('mission');
    $frontpage = true;
  }

151 152 153 154 155
  /* Add favicon */
  if (theme_get_setting('toggle_favicon')) {
    drupal_set_html_head('<link rel="shortcut icon" href="'. check_url(theme_get_setting('favicon')) .'" type="image/x-icon" />');
  }

156 157 158
  /**
  * Populate sidebars.
  */
159
  $layout = 'none';
160 161
  global $sidebar_indicator;
  /**
162
   * Sidebar_indicator tells the block counting code to count sidebars separately.
163 164
   */
  $sidebar_indicator = 'left';
165 166 167
  $sidebar_left = theme('blocks', 'left');
  if ($sidebar_left != '') {
    $layout = 'left';
168 169 170
  }

  $sidebar_indicator = 'right';
171 172 173
  $sidebar_right = theme('blocks', 'right');
  if ($sidebar_right != '') {
    $layout = ($layout == 'left') ? 'both' : 'right';
174
  }
175
  $sidebar_indicator = NULL;
176

177 178 179 180 181 182 183 184 185 186 187
  // Construct page title
  if (drupal_get_title()) {
    $head_title = array(strip_tags(drupal_get_title()), variable_get('site_name', 'drupal'));
  }
  else {
    $head_title = array(variable_get('site_name', 'drupal'));
    if (variable_get('site_slogan', '')) {
      $head_title[] = variable_get('site_slogan', '');
    }
  }

188
  $variables = array(
189
    'base_path'           => $base_path,
190 191 192
    'breadcrumb'          => theme('breadcrumb', drupal_get_breadcrumb()),
    'closure'             => theme('closure'),
    'content'             => '<!-- begin content -->' . $content . '<!-- end content -->',
193
    'footer_message'      => variable_get('site_footer', FALSE) . "\n" . theme('blocks', 'footer'),
194
    'head'                => drupal_get_html_head(),
195
    'head_title'          => implode(' | ', $head_title),
196
    'help'                => theme('help'),
197
    'language'            => $GLOBALS['locale'],
198
    'layout'              => $layout,
199
    'logo'                => theme_get_setting('logo'),
200
    'messages'            => theme('status_messages'),
201
    'mission'             => isset($mission) ? $mission : '',
202
    'primary_links'       => menu_primary_links(),
203
    'search_box'          => (theme_get_setting('toggle_search') ? search_box() : ''),
204
    'secondary_links'     => menu_secondary_links(),
205 206
    'sidebar_left'        => $sidebar_left,
    'sidebar_right'       => $sidebar_right,
207 208
    'site_name'           => (theme_get_setting('toggle_name') ? variable_get('site_name', 'Drupal') : ''),
    'site_slogan'         => (theme_get_setting('toggle_slogan') ? variable_get('site_slogan', '') : ''),
209 210 211
    'styles'              => theme_get_styles(),
    'tabs'                => theme('menu_local_tasks'),
    'title'               => drupal_get_title()
212
  );
213 214

  if ((arg(0) == 'node') && is_numeric(arg(1))) {
215
    $variables['node'] = node_load(arg(1));
216 217 218 219 220
  }

  return _phptemplate_callback('page', $variables);
}

221
/*
222 223 224
 * Prepare the values passed to the theme_node function to be passed
 * into a pluggable template engine.
 */
225
function phptemplate_node($node, $teaser = 0, $page = 0) {
226 227 228 229 230 231 232 233
  if (module_exist('taxonomy')) {
    $taxonomy = taxonomy_link('taxonomy terms', $node);
  }
  else {
    $taxonomy = array();
  }

  $variables = array(
234
    'content'        => ($teaser && $node->teaser) ? $node->teaser : $node->body,
235 236
    'date'           => format_date($node->created),
    'links'          => $node->links ? theme('links', $node->links) : '',
237
    'name'           => theme('username', $node),
238 239 240 241
    'node'           => $node,  // we pass the actual node to allow more customization
    'node_url'       => url('node/'. $node->nid),
    'page'           => $page,
    'taxonomy'       => $taxonomy,
242
    'teaser'         => $teaser,
243 244 245
    'terms'          => theme('links', $taxonomy),
    'title'          => check_plain($node->title)
  );
246

247
  // Flatten the node object's member fields.
248
  $variables = array_merge((array)$node, $variables);
249

250 251
  // Display info only on certain node types.
  if (theme_get_setting('toggle_node_info_' . $node->type)) {
252
    $variables['submitted'] =  t('Submitted by %a on %b.', array('%a' => theme('username', $node), '%b' => format_date($node->created)));
253
    $variables['picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '';
254 255 256 257 258 259 260 261 262 263 264
  }

  return _phptemplate_callback('node', $variables, '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(
265
    'author'    => theme('username', $comment),
266 267
    'comment'   => $comment,
    'content'   => $comment->comment,
268
    'date'      => format_date($comment->timestamp),
269
    'links'     => isset($links) ? theme('links', $links) : '',
270 271 272
    'new'       => $comment->new ? t('new') : '',
    'picture'   => theme_get_setting('toggle_comment_user_picture') ? theme('user_picture', $comment) : '',
    'submitted' => t('Submitted by %a on %b.',
273
                      array('%a' => theme('username', $comment),
Steven Wittens's avatar
Steven Wittens committed
274
                            '%b' => format_date($comment->timestamp))),
275
    'title'     => l($comment->subject, $_GET['q'], NULL, NULL, "comment-$comment->cid")
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
  ));
}

/**
 * Prepare the values passed to the theme_block function to be passed
 * into a pluggable template engine.
 */
function phptemplate_block($block) {
  return _phptemplate_callback('block', array('block' => $block));
}

/**
 * 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,
294 295
    'region'  =>   $region,
    'title'   =>   $title
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
  ));
}

/**
 * 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 $file
 *   A suggested template file to use.
 */
314
function _phptemplate_default($hook, $variables, $file = NULL) {
315
  if (!empty($file) && file_exists(path_to_theme() . "/$file.tpl.php")) {
316 317 318 319 320 321 322 323 324 325 326 327
    $file = path_to_theme() . "/$file.tpl.php";
  }
  else {
    if (file_exists(path_to_theme() . "/$hook.tpl.php")) {
      $file = path_to_theme() . "/$hook.tpl.php";
    }
    else {
      if (in_array($hook, array('node', 'block', 'box', 'comment'))) {
        $file = "themes/engines/phptemplate/$hook.tpl.php";
      }
      else {
        $variables['hook'] = $hook;
328
        watchdog('error', t('PHPTemplate was instructed to override the %name theme function, but no valid template file was found.', array('%name' => theme('placeholder', $hook))));
329
        $file = 'themes/engines/phptemplate/default.tpl.php';
330 331 332 333
      }
    }
  }

334 335
  if (isset($file)) {
    extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
336
    ob_start();                      // Start output buffering
337
    include "./$file";               // Include the file
338 339 340
    $contents = ob_get_contents();   // Get the contents of the buffer
    ob_end_clean();                  // End buffering and discard
    return $contents;                // Return the contents
341 342 343 344 345
  }

}

?>