Return to Snippet

Revision: 3479
at July 30, 2007 12:48 by berkes


Initial Code
/**
 * Make additional variables available and override some core variables
 */
function _phptemplate_variables($hook, $vars) {
  switch ($hook) {
    case 'page': 
      $vars['page_class'] = (($vars['is_front']) ? 'front' : _MyTheme_css_safe(arg(0)) );

      if (in_array($vars['page_class'], _MyTheme_list_top_level_classes())) {
        $vars['has_menu_image'] = TRUE;
      }
    break;
   }

  return $vars;
}

/**
 * Prepare the specified string for use as a CSS identifier.
 */
function _MyTheme_css_safe($string, $length = 36) {
  // Replace or drop apostrophes based on user settings
  $separator = '-';

  // Preserve alphanumerics, everything else becomes a separator
  $pattern = '/[^a-zA-Z0-9]+/ ';
  $output = preg_replace($pattern, $separator, $string);

  // Trim any leading or trailing separators (note the need to
  // escape the separator if and only if it is not alphanumeric)
  if ($separator) {
    if (ctype_alnum($separator)) {
      $seppattern = $separator;
    }
    else {
      $seppattern = '\\'. $separator;
    }
    $output = preg_replace("/^$seppattern+|$seppattern+$/", "", $output);
  }

  // Enforce the maximum component length
  $output = drupal_substr($output, 0, $length);

  return $output;
}

/**
 * List with toplevel classes
 * @TODO: you probably want to make this configurable somewhere;
 **/
function _MyTheme_list_top_level_classes() {
  $list = array(
    'node',
    'taxonomy',
    'user',
    'profile',
  );
  return $list;
}

/**
 * The following takes place in page.tpl.php between 11PM and 12PM;
 **/
<?php
//....
<body class="<?php print $page_class ?>">
//....
    <?php if (!$is_front && $has_menu_image): ?>
       <h2 class="content title" id="content-title-image"><?php print $title ?></h2>
    <?php endif; ?>

Initial URL


Initial Description
Put classes around your bodyy, to allow different colors for seperate pages and areas on your site.
IT will simply add a css-safe version of the first argument in an url. Say the url is /about_us/our_company the class will be
body.about-us
Requires phptemplate

Initial Title
Put classes around your body

Initial Tags
css, php, drupal, theme

Initial Language
PHP