How would you please use the template.php file to check whether the right sidebar is present or not and then display a 3 column or 2 column layout accordingly?

We are technically not supposed to do any php logic inside our page.tpl.php so I could NOT do the following:

if($right !=""): //if right nav is present, 

<div id="three_column">...</div>// then display a 3 column layout
else:
<div id="two_column">...</div> //display a 2 column instead.

//The above is wrong because it's highly recommended NOT to put php logic inside page.tpl.php but rather //use theme function overrides in our template.tpl.php where most of the php logic would typically go. 

//So, how would I implement the same functionality for displaying a 2 or 3 column layout based on whether //there was a right nav or not OR based on anything, using the template.tpl.php and NOT what I did //above??????

A good solution would show only code in template.tpl.php :)

Thanks guys, I really need help out here. Thx again.

Comments

nevets’s picture

A number of themes use approach similiar to the one found in the Fusion theme. In template.php they implement hook_preprocess_page() and add a 'body_classes' value that holds one or more classes that represent information about the page including the presence/absence of columns. The variable is then printed as the class value for the body tag and the classes used to conditional style the layout.

joelfoit’s picture

Can I do something like this?
I am just a drupal newbie but I learned some php and it's helped me picked up a great deal, plus the community, you guys, are incredibly helpful.
So, can I do this?

You could call the code below sudo code, I am just laying out the idea I have in mind, this might not be the right synthax.

//template.php
function mytheme_preprocess_page(&$vars) {
$vars['class_2']=two_column;
$vars['class_3']=three_column;

if(!$right) //if the right sidebar is not there, then go ahead and print out three column, otherwise print a 2 col
     return $vars['class_3'];
else
    return $vars['class_2'];

And then back over to the page.tpl.php

//page.tpl.php
<div id="<?php print mytheme_preprocess_page(&$vars); 

">content here

?>
The idea here is this div would inherit or be passed either class_2 for a 2 column layout in my css or class_3 based on the php logic back in the template.php file.

Do you hopefully get a better idea of what I am trying to do here?
Help much appreciated !
Thx

nevets’s picture

The tpl file would use something like

<div id="<?php print $column_id?>">

and in template.php something like

function mytheme_preprocess_page(&$vars) {
if ( !empty($vars['right']) ) {
  $vars['column_id'] = 'three_column';
}
else {
  $vars['column_id'] = 'two_column';
}
}
joelfoit’s picture

Thank you so much,
I will try that and see what happens. So far, the function works in my template.php but the $colum_id variable is empty, it doesn't print anything so I get something like

where $colum_id is empty.
I am gonna see if I can print out the content of $vars that r passed in to see which specific variable is responsible for outputting the id field. something like this:

print_r($vars);
//or use the dsm function from devel to print it out.
dsm($vars);
//But if u have any idea why the $colum_id variable stays empty, please let me know.

//Joel