Embed a Region in a node.tpl.php file (D5)
By default, drupal provides a number of regions to put blocks in and lets you put them pretty much anywhere in the page you want to. But, there are a few exceptions to this rule. What if you want to put a region in a node? What if you want to put a region in between a node and the comments on that node? This may not seem so easy out of the box. Let's take a look at how to put a region in a node.tpl.php file.
Let's walk through a situation where you want to put a custom region between a node and it's comments. YOu could use this to place something like a block from the Similar by Terms module right under my posting.
Start by adding a custom region called node_region using hook_regions() in your themes template.php file.
<?php
function mythemename_regions() {
return array(
'left_sidebar' => t('left sidebar'),
'right_sidebar' => t('right sidebar'),
'header' => t('header'),
'footer_message' => t('footer'),
'content' => t('content'),
'node_region' => t('node region'),
);
}
?>You'll notice the same regions that come by default are defined here as well as the new node_region. This function defines all the regions, not just additional ones. In this case, you defined the same ones that come by default and just added my additional region.
This now created the node_region so you can assign blocks to in on your blocks admin screen.
Next you need to make the variable $node_region accessible in the node.tpl.php file. To do this use the function _phptemplate_variables.
<?php
function _phptemplate_variables($hook, $variables) {
if ($hook == 'node') {
$variables['node_region'] = theme('blocks', 'node_region');
}
return $variables;
}
?>This will made $node_region available in node.tpl.php files. This same concept applies for comments and most of those other non-intuitive places you may want to put a block where it isn't obvious how.
Note: This works for drupal 5. Implementing this is slightly different in drupal 6.

Drupal 6 way
The way you would do the same in Drupal 6 is to modify your theme.info file with regions you want to use:
regions[header] = Headerregions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[footer] = Footer
regions[node_region] = Node region
Next you have to implement a preprocess function in your template.php file. For example:
function phptemplate_preprocess(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
This will enable you to use $node_region in your node.tpl.php file.
Drupal 6 preprocess_node
You can use the preprocess_node function specifically as well and you have to if using a subtheme like for Zen.
function subtheme_preprocess_node(&$variables) {$variables['node_region'] = theme('blocks', 'node_region');
}
in Zen $vars instead of $variables
for me in Zen $vars worked instead of $variables
phptemplate_preprocess_node
I'm pretty sure this:
function phptemplate_preprocess(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
needs to be:
function phptemplate_preprocess_node(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
or
function yourthemename_preprocess_node(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
Last function worked for me
Thank You very much Jacine!
Jacine: your example worked
Jacine: your version worked swimmingly for D6. Thanks!
kind of
hey -
i setup the new region and it appears and everything, but it still keeps being placed BELOW the comments.
i can go into the node.tpl.php and hand code some HTML in there and it shows up fine where its supposed to.
i'm on drupal 6.x with the barron theme.
this is driving me nuts.
austin, texas - beyoatch.
I also used Jacine's approach
I also used Jacine's approach and works well.
I used in the template.php file:
function yourthemename_preprocess_node(&$variables, $hook) {$variables['node_region'] = theme('blocks', 'node_region');
}
and then I used $node_region in node.tpl.php file just after the $content printed like that:
<div class="content">
<?php echo $content; ?>
</div>
<div class="node_region">
<?php echo $node_region; ?>
</div>
}
Now blocks appear between content and comments as intended.
Hope it helps...
Sinan
I don't get this to work. My
I don't get this to work. My problems are exactly the same like the ones of nathanmcginty.
I added this to my template.php:
<?phpfunction mw_preprocess_node(&$variables, $hook) {
$variables['below_content'] = theme('blocks', 'below_content');
}
?>
"mw" is my template and "below_content" is the region. Shouldn't it now be possible to print this in my node.tpl.php?
<?phpprint $below_content
?>
If I write this in my node.tpl.php nothing happens.
Please help,
Nico
theme name?
Are you sure your theme name goes as "mw"? maybe it is something about that... Just a suggestion...
Sinan
If your using Zen the
If your using Zen the preprocess function are commented out. Can't believe I spent 15mins trying to get this to work before I realised.
template.php error
I'm getting this error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING in mytheme/template.phpmarcopolo ---
Blog Marco | Best Designed Drupal Websites