Let's say in page.tpl.php I want to detect which content type and print out a different block, depending on Content type:

if($contenttype='events'){
do this
}

how would I do something like that in the page.tpl.php file?

Thanks

Comments

nevets’s picture

if  ( !empty($node) && $node->type == 'events'  ) {
 //  do  that
}
neokrish’s picture

does $node exist in page.tpl.php? If I am not wrong, $node variable dies out after node.tpl.php is parsed. right? I agree that the above code will work for sure in node.tpl.php but doubtful for page.tpl.php.

nevets’s picture

In page.tpl.php, if the path is of the form node/{nid} (or node/{nid}/*) $node is defined.

neokrish’s picture

thanks nevets for taking time to explain me this. you are superfast!

Jordash’s picture

I was thinking of that way but it doesn't seem like the cleanest way to do it, you'd think drupal would have exposed this type of variable it could come in very handy.

I don't think you can print out blocks in the node.tpl.php unless I'm missing something.

nevets’s picture

While one can print blocks in page.tpl.php, it is not considered best practice.

It sounds like what you want to do is only show the block when the page is displaying content of type 'events'. You can do that by adding the block to the appropriate region, configuring the block and limiting visibility with PHP code. This code would work

<?php
$node = menu_get_object();
if ( !empty($node) && $node->type == 'events'  ) {
  return TRUE;
}
return FALSE;
?>
Jordash’s picture

@nevets

if ( !empty($node) && $node->type == 'events' ) {
// do that
}

That worked perfectly It does work in the page.tpl.php page

Thanks for the tip.