We offer affordable conversion services to convert from PSD to Drupal.
Submit your own design composition to us to start building your Drupal site now.
Submit your brief for custom modules that you'd like us to build.

Drupal 7 - Printing block region in node.tpl.php

Article by duckz
created at 12. Jan, 2011

Drupal 7 will consider "node" $content as a block region, which has advantages over the old "special" $content variable in Drupal 6.

Now since the $content is a block region, themer no longer needs to provide special region area in top / bottom of the node, a site builder just have to put the "top node" block in the content region and set it to be on top / bottom of the content block.

But how about if you want to "integrate" the content node block with other block region, like if you want to display a "node" content on left top while providing another custom region on the left bottom of the node while printing the rest of the node content on the right side??

Sounds complex? In drupal 6, we can achieve this by invoking

<?php
theme
('blocks''your_block_region_machine_name');
?>

Now in Drupal 7, theme_blocks is gone... so to achieve the same result we need to invoke this instead :

<?php
print render(block_get_blocks_by_region('your_block_region_machine_name'));
?>

The better way to get this done in Drupal 7 is by invoking the function inside preprocess_node function like this :

<?php
function your_theme_name_preprocess_node(&$variables) {
    if (
$blocks block_get_blocks_by_region('your_block_region_machine_name')) {
      
$variables['banner_space'] = $blocks;
  }
}
?>

Additional information provided by aquariumtap if you want to preprocess the block region directly from template.php instead of theme.info files :

aquariumtap :

If you want those blocks to be available to hook_preprocess_region, add a bit more information:

<?php
function your_theme_name_preprocess_node(&$variables) {

if (
$blocks block_get_blocks_by_region('your_block_region_machine_name')) {
  
$variables['banner_space'] = $blocks;
  
$variables['banner_space'] = $blocks;
  
$variables['banner_space']['#theme_wrappers'] = array('region');
  
$variables['banner_space']['#region'] = 'your_block_region_machine_name';
  }
}
?>

then invoke this function inside your node.tpl.php

1
<?php print render($banner_space); ?>

If you got better way to do this, please post it in your comment :)

14 comments
Brent (not verified) say:
Fri, 05/04/2012 - 06:23
Brent's picture

I've been searching this code many times but I didn't found it,thanks I found this sites

Printing

Jennifer (not verified) say:
Fri, 02/10/2012 - 06:32
Jennifer's picture

Could you be so kind to show me how to put 2 or 3 blocks in bottom 1 region in Drupal 7 theme.

BadDaddy (not verified) say:
Tue, 09/13/2011 - 21:44
BadDaddy's picture

Awesome, exactly what I needed! Thanks

duckz say:
Mon, 08/01/2011 - 18:27
duckz's picture

Adding this code in template.php

$variables['foo'] = block_get_blocks_by_region('your_block_region_machine_name');
?>

and when rendering it in tpls (eg node.tpl)

print render($foo);
?>

is optional and not required.

if you want to conditionally rendering you'll need to split the function :

$block_region block_get_blocks_by_region('your_block_region_machine_name');

if (isset(
$block_region)) { // you'll need to double check the actual return value for the $block_region, this is just an example and it may not work
 
print render($block_region);
}

?>

aiphes (not verified) say:
Mon, 08/01/2011 - 10:13
aiphes's picture

ok it works but no need to add code in template.php ? your ways works but how can do this code conditionnaly rendered by php if region isnt empty ?

thanks

duckz say:
Sat, 07/30/2011 - 06:24
duckz's picture

edit the theme .info to register your multiple custom block regions, then print each of the regions in the node.tpl (or other place) by invoking :

print render(block_get_blocks_by_region('your_block_region_machine_name'));
?>

for each block region that you want to print

aiphes (not verified) say:
Fri, 07/29/2011 - 11:55
aiphes's picture

hi

how to do to have multiple regions in D7 , i've posted here : http://drupal.org/node/1232500

if someone could take a look to ..

thanks

aiphes (not verified) say:
Fri, 07/29/2011 - 09:24
aiphes's picture

hi

how to do to have multiple regions in D7 , i've posted here : http://drupal.org/node/1232500

if someone could take a look to ..

thanks

duckz say:
Sat, 05/07/2011 - 09:15
duckz's picture

Thanks for providing the code, I was assuming people will create the regions from the theme.info files since they are capable to edit the template.php files.

But your code will provide user with more options that they can actually create a block region without the need to edit .info file.

aquariumtap (not verified) say:
Sat, 05/07/2011 - 01:10
aquariumtap's picture

If you want those blocks to be available to hook_preprocess_region, add a bit more information:

$variables['banner_space'] = $blocks;
$variables['banner_space']['#theme_wrappers'] = array('region');
$variables['banner_space']['#region'] = 'your_block_region_machine_name';

Rolainfalay (not verified) say:
Tue, 04/12/2011 - 16:32
Rolainfalay's picture

Nice blog here! Also your website loads up fast! What host are you using? I wish my website (Papper) loaded up as fast as yours lol

Nubimmorybymn (not verified) say:
Tue, 04/12/2011 - 10:22
Nubimmorybymn's picture

Wow!, this was a real quality post. In theory I'd like to write like this too - taking time and real effort to make a good article on Kosttillskott... but what can I say... I keep putting it off and never seem to get something done.

Anonymous (not verified) say:
Mon, 03/28/2011 - 12:40
Anonymous's picture

Excellent! Just what I was looking for :)

jive (not verified) say:
Tue, 02/22/2011 - 17:36
jive's picture

I was searching for this very thing. Thanks! works like a charm!

Post new comment

The content of this field is kept private and will not be shown publicly.
Image CAPTCHA
Enter the characters shown in the image.
To prevent automated spam submissions leave this field empty.