Drupal allows very fine grained control over how content is presented to the end user. The presentation layer is abstracted beautifully and cleanly designed hooks allows for moving data from the logic layer to the presentation layer. The template file that handles the basic page layout of every drupal site is page.tpl.php and the different regions available for a given theme are all available as HTML elements in page.tpl.php. Another important template file is node.tpl.php which decides how nodes are presented to the end user. Normally the content of nodes go into regions inside page.tpl.php and so does blocks and panels. But what if you want to have regions inside the display area of nodes? What if you want to use blocks to display content inside nodes? The answer is simple - template preprocess is the key.
First let us define the region in the theme info file. Open the info file in your theme folder.
If no regions[] are defined you will have define all the default regions in the info file explicitly if you want to add new custom regions. Copy and paste the following default regions (as of Drupal 6) into the file
regions[left] = Left sidebar
regions[right] = Right sidebar
regions[content] = Content
regions[header] = Header
regions[footer] = Footer
Now add the declaration of your brand new region. Suppose you plan to use cool_node_region as the name of the region. Note that the right side of the equal sign is the human readable name of the region that you are going to see at admin/build/blocks
regions[cool_node_region] = Cool Node Region
If you already see regions[] variables, all you have to do is to add your new region. Then go ahead and add your custom region to the node.tpl.php. Create the HTML layout and add the php code to output the content of the region into the layout. In our example you will have to create something like the following somewhere in node.tpl.php
<div class="cool-node-region"><?= $cool_node_region; ?></div>
Remember to set appropriate css classes and ids so that you can use these to style the content in these regions via your stylesheet and not have to edit your template files again.
Create the preprocess_node function in template.php file in your theme folder
/**
* preprocess hook to add variables to the node.tpl.php
*/
function theme_preprocess_node(&$variables) {
$variables['cool_node_region'] = theme('blocks', 'cool_node_region');
}
Rebuild your theme registry and presto you are done. To rebuild your theme registry you can use the devel module or just go to admin/build/themes and save the form without making any changes. You can verify that your new region is active by going to admin/build/blocks.



Comments
It looks like you have not
Anonymous (not verified) wrote on September 3, 2012 - 00:51It looks like you have not used pre tags for the code and that is why you have lost the indentation.
Thank you , it was great help
Aya Younis (not verified) wrote on August 29, 2012 - 19:51Thank you , it was great help :)
Custom regions in node.tpl.php drupal 7
vimal joseph (not verified) wrote on April 11, 2012 - 09:26You can use $variables['cool_node_region']= block_get_blocks_by_region('cool_node_region') in the preprocess_node hook and print render($cool_node_region) in the node.tpl.php
Just switched to Drupal and i
sasha grey (not verified) wrote on August 13, 2011 - 09:34Just switched to Drupal and i find it quite confusing at first. However, thanks to your post everything seems much more simple now.
Great stuff
Gab (not verified) wrote on July 1, 2011 - 01:17Thanks for the advice
I agree with the entire
Jake (not verified) wrote on August 13, 2011 - 19:37I agree with the entire comment above. Thanks for sharing nice information with us. like your post and all you share with us is up todate and quite informative, i would like to bookmark the page so i can come here again to read you, as you have done a wonderful job.