[Drupal] How to create regions for a Drupal websites?

| | 2 min read

When we try to theme a Drupal website certain case we need to create particular region in the Drupal website. Adding region for a Drupal website is not a hard task, even a developer first creating a Drupal website can create a region. In order to add regions inside a Drupal website we only need to add a few lines extra for that. Below there are three version of drupal for the drupal 6 and 7 version creating the region is same but for the Drupal 5 version it is slightly different.

Adding regions to drupal 7.x and drupal 6.x versions

ADD the following code to you mytheme.info file:

regions[left] = Left sidebar
  regions[right] = Right sidebar
  regions[content] = Content
  regions[header] = Header
  regions[footer] = Footer
  regions[new_region_name] = New Region Name

The name inside the square bracket "[]" is the machine readable name and the value given is the human readable name.

By adding the above content inside the .info file will create the region.

In order to get the region we need to print the new region in your page.tpl.php file.

The below is the sample code for printing the region

<?php if ($page['new_region_name']): ?>
      <div id="new_region_name" ><div class="section">
        <?php print render($page['new_region_name']); ?>
      </div>></div> <!-- /.section, /#new_region_name -->
    <?php endif; ?>

if we create a region we need to clear the cache otherwise we won't get the new region.For clearing the cache

Goto "Administer > Site configuration > Performance" and click the "Empty cache" link

Adding region inside the Drupal 5.x version

For adding a region inside the drupal 5 version only need to add the below code inside the template.php file


            /**
             *
             * Adding new region 
             **/
              function framework_regions() {
                return array(
                  'left' => t('left sidebar'),
                  'right' => t('right sidebar'),
                  'content' => t('content'),
                  'header' => t('header'),
                  'footer' => t('footer'),
                  'new_region_name' => t('new region name'),
                );
              }
    

In order to get the region we need to print the new region in your page.tpl.php file.

The below is the sample code for printing the region

<?php if ($page['new_region_name']): ?>
      <div id="new_region_name" ><div class="section">
        <?php print render($page['new_region_name']); ?>
      </div>></div> <!-- /.section, /#new_region_name -->
    <?php endif; ?>