Skip to content
phptemplate.engine 2.26 KiB
Newer Older
<?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)) {
 * Implementation of hook_theme 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) {

  // 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');
}

/**
 * Adds additional helper variables to all templates.
 *
 * Counts how many times certain hooks have been called. Sidebar left / right are special cases.
 *
 * @param $variables
 *   A series of key-value value pairs.
 * @param $hook
 *   The name of the theme function being executed.
 */
function phptemplate_engine_preprocess(&$variables, $hook) {
  // 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]++;

  // Tell all templates where they are located.
  $variables['directory'] = path_to_theme();
  $variables['is_front'] = drupal_is_front_page();