HowTo: build your frontpage with regions

A nice feature in Drupal 4.7 and newer is that you can define regions. A region is normally a global thing, appearing on every page, but with some creativity you can use this technology to build complex (front) pages.

Note that this is for use with PHPTemplate themes (the default theme engine in Drupal.) There is a page for Plain PHP themes to do something similar.

Let us assume we want three rows on the frontpage, frontpage top, frontpage center and frontpage bottom. And let us assume we have a custom theme called bimbam. In the examples, you should replace bimbam with the name of your theme. If you call your theme 'jungle_fever', then just replace all occurrences of 'bimbam' with 'jungle_fever'.

  1. We add the following to our template.php in themes/bimbam/ (create the this file if you don't have one already). This creates three new regions.
    <?php
    /** Define the regions **/
    function bimbam_regions() {
      return array(
           
    'left' => t('left sidebar'),
           
    'right' => t('right sidebar'),
           
    'content' => t('content'),
           
    'header' => t('header'),
           
    'footer' => t('footer'),
           
    'frontpage_top' => t('frontpage top'),
           
    'frontpage_center' => t('frontpage center'),
           
    'frontpage_bottom' => t('frontpage bottom'),
       );
    }
    ?>
  2. Now visit admin >> block, and you will see that for the bimbam theme, the select lists for each block contain three new options, frontpage top, frontpage center and frontpage bottom.
    ?>
  3. These do not yet print, though, because you still need to print the regions in your pages. To do this, open page.tpl.php and add the following:
        <?php if ($is_front) : ?>
        <div id="frontpage_top" class="frontpage">
          <?php print $frontpage_top ?>
        </div>
        <?php endif; ?>

    The surrounding if() is there to prevent the region from printing anywhere except the frontpage.
    However, this also prevents the region from printing on the block config page. So we must fine tune this a little.
  4. We add more logic to make it show on the blocks administration too:
        <?php if ($is_front || strstr($_GET['q'], 'admin/block')) : ?>
        <div id="frontpage_top" class="frontpage">
          <?php print $frontpage_top ?>
        </div>
        <?php endif; ?>
  5. Repeat this for all your three frontpage regions. If you need nothing in between two regions, put them in one if(). That will gain you speed and performance.
  6. Now with some more HTML (tables) or CSS, you can place these regions anywhere on your frontpage, make them look nicer, etc.
  7. If you want the default content not to print on the frontpage, you should surround the <?php print $content ?> with an if (!$is_front).
  8. Visit the blocks repository for nice blocks, or create them with views so that you can put them on your frontpage.

Enjoy your nice frontpage!

hook_regions() DEPRICATED IN DRUPAL 6.X

michaelphipps - March 13, 2008 - 06:08

PLEASE NOTE HOOK_REGIONS() NO LONGER WORKS IN DRUPAL 6.X

Templates now have .info files where this information is stored. You should refer to the new documentation at http://drupal.org/node/132442#defining-regions explaining how to define regions in Drupal 6.0

 
 

Drupal is a registered trademark of Dries Buytaert.