Printing classes for a theme depending on sidebar

by Benjamin Melançon
Posted on Mon, 2008-04-07 12:08

Key words and phrases

phptemplate variables show body class layout

Tags

Description

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'page':
     
// our own hook to remove sidebars from admin pages, not relevant here
     
if (arg(0) == 'admin') {
       
$vars['sidebar_right'] = '';
      }
     
     
// layout for body classes
     
$vars['layout'] = '';

     
$layout = '';
     
//Is there a left sidebar?
     
if ($vars['sidebar_left'] != '') {
         
$layout = 'sidebar-left';
      }
     
//Is there a right sidebar?
     
if ($vars['sidebar_right'] != '') {
          
$layout = ($layout == 'sidebar-left') ? 'sidebars' : 'sidebar-right';
      }
      if (
$layout != ''){
        
$vars['layout'] .= ' ' . $layout;
      }
      break;     
  }
  return
$vars;
}
?>

And if you want to see everything that's going on, this can be thrown in before the return statement, or at the top, to see what's there before you mess with it:

<?php
global $user; if ($user->uid == 1drupal_set_message('<pre>'. var_export($vars,TRUE) .'</pre>');
?>

(Internal Agaric note: Instead of a note to remove if we truly never use left sidebars, which we do not, we just link here on how to put this more complex code back.)

Post new comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <small> <pre> <strike> <sub> <sup> <kbd> <s>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.