Need some help with your project? Contact me

How to add a region to your node template in Drupal 7

This tutorial will show you how to add a region to your Node template. That is, how to display, or call, a region in your node.tpl.php (rather than in your Page template: page.tpl.php). This might come in handy when for instance you want to display a block somewhere inside your node. I used it once to add a region for blocks at the end of my node content, but before the Comment section (which in the page.tpl.php is generally included in the main content variable).

Just like adding a new region to your Page template, you will first need to declare a new region in your theme.info file.

Next, you need to edit your node.tpl.php file (you can find that in your theme folder, possibly in the templates sub-folder). If you have custom node templates for specific Content Types and you want to call a new region in those, edit those (too).

Try to find this .php line: print render($content); This renders out the content of your node (which includes also additional fields that you added with CCK). You can use this to orient yourself and figure out where you want to display your new region. And the command to call your new region is:

print render(block_get_blocks_by_region('machine_name_of_your_region'));

print is simply the .php command that writes the content on the page whereas the next part is what is important in our case. You can wrap it in .html to have it displayed however you want. Then you can save and close.

Next, you just add a block to your new region and it should appear where you added it on your node.

Hope this helps.

NB: A good way to later manage your block display in regions is using the Context module.

Further information on the topic?

Comments

This doesn't seem to work with blocks assigned via context. Anyway, here's a solution:
https://drupal.org/comment/5354288#comment-5354288

Amazing!
Thanks for the time to explain it. Exactly what I search for.

thanks for sharing mr.

Yes! It's big and great of idea.
Thank's.

Nice post. But since rendering regions directly will shoot an error message, you should declare it before rendering like so:

<?php
$region = block_get_blocks_by_region('lside-bottom');
print render($region);
?>

(from: http://drupal.stackexchange.com/a/23216/3125)

Yes, indeed, and yes, this solves the error.

Thanks to all who contributed here.

Thank you very much, it's done for me! No error ;-)

Add new comment

You can post comments in Markdown and basic HTML tags.
For code blocks, wrap your code within '~~~'. For example:
~~~
$var = 'my variable';
~~~