How to create custom regions in node.tpl.php in Drupal 6
BY webmaster
9 years ago
comments comment

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.



on 09th April 2007 / by webmaster
Freelance projects fall into a separate category of projects in that they target independent deve Community Projects Freelance Open Source Web Development Leave a reply Your email address will not be published. Required fields are marker * Shrinivasan (not verified) access_time 16 Jul 2019 - 05:51 Guys. Great Collection and a great site. It is really impressive to know that there a numerous FOSS tools for web contents. Thanks. Keep Growing. Anonymous (not verified) access_time 16 Jul 2019 - 05:51 In reply to Great collection by Shrinivasan (not verified) Great Collection and a great site!!! Thanks! Silvia (not verified) access_time 16 Jul 2019 - 05:51 Hi, nice work. I also love to use WordPress when setting up a new blog. Greetings JungleDirector (not verified) access_time 16 Jul 2019 - 05:51 Subj. Anybody have? I'ts better, if you have XRumer 5.04... Gimme link Thanks. Ingo (not verified) access_time 16 Jul 2019 - 05:51 Hi, this is a listing of open source software was looking for my new project Thx a lot Pagination Current page 1 Page 2 Next page Next › Last page Last » Add new comment
Leave a reply
Your email address will not be published. Required fields are marker *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.

Gab (not verified)
access_time 16 Jul 2019 - 05:51

Thanks for the advice

Jake (not verified)
access_time 16 Jul 2019 - 05:51

In reply to by Gab (not verified)

I 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.

sasha grey (not verified)
access_time 16 Jul 2019 - 05:51

Just switched to Drupal and i find it quite confusing at first. However, thanks to your post everything seems much more simple now.

vimal joseph (not verified)
access_time 16 Jul 2019 - 05:51

You 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

Aya Younis (not verified)
access_time 16 Jul 2019 - 05:51

Thank you , it was great help :)