Drupal Tutorials

Custom Drupal theme regions

This tutorial requires you have basic to intermediate knowledge of Drupal theming, basic knowledge of PHP and an understanding of HTML and CSS.

From Drupal 4.7 onwards Drupal themers now have the ability to set their own custom block regions for their themes. This gives even more power to your theme and your ability to customize your themes.

Blocks admin areaBy regions I mean the regions in the page template where you can assign block content like left-sidebar,right-sidebar, content etc. Just take a look at the blocks admin area (yoursite.com?q=admin/build/block) on your site and you’ll see something similar to the image on the right. But these can default regions can be a bit restrictive so let's take a look at how you define your own custom block regions for your theme.

The template.php code

In your themes template.php file is where you define all the block regions you want using code similar to this:

<?php
function YOUR_THEMES_NAME_regions() {
  return array(
   
'left' => t('left sidebar'),
   
'right' => t('right sidebar'),
   
'content' => t('content'),
   
'header' => t('header'),
   
'footer' => t('footer'),
   
'user_profile_left' => t('user profile left'),
   
'user_profile_right' => t('user profile right'),
   
'header_advert' => t('Header Advert'),
   
'content_top' => t('Content Inner Top'),
   
'footer_col1' => t('footer column 1'),
   
'footer_col2' => t('footer column 2'),
   
'footer_col3' => t('footer column 3'),
  );
}
?>
This is the exact regions I am using on DrupalSN, you'd obviously need to change the YOUR_THEMES_NAME bit to the title of your theme which is whatever you called the folder your theme lives in, mines druplsn_v1.

The code returned by this function is just a simple array with the key (on the left hand side) being the computer friendly name so you shouldn't use spaces or any strange characters and the value (on the right hand side) is the human readable name that you see in the blocks admin area drop down menus.

The page.tpl.php code

Ok so we've defined our block regions now it's time make them show up in our template and this is really simple.

  1. We going to be working with the page.tpl.php file, so go ahead and open that up in your favourite text editor (Notepad or Dreamweaver).
  2. Now refer back to the regions array we defined in the template.php file and simply take the key name and put that into a PHP echo statement with a $ sign in front of the key name like so:
    <?php
    echo $KEY_NAME;
    ?>
  3. So let's put our "left sidebar" region into the left sidebar of our layout. The key for the "left sidebar" region is "left" so our code would look like this:
      <div class="sidebar_left">
        <?php echo $left; ?>
      </div>
    It's that simple! This bit of code places the "left" region within a div with the class "sidebar_left" and this corresponds to some CSS layout code that maybe look something like this:
    .sidebar_left{
      width:25%;
      float:left;
      margin-right:2%;
      overflow: hidden;
    }

You can take this same principle to fill out your whole page layout with your custom regions.

Test the layout

Once you've added all your regions to your page.tpl.php you need to test they are working by assigning block content to the regions via the blocks admin area.

So, go to the blocks admin area (yoursite.com?q=admin/build/block) and assign a few blocks to your new regions and save the settings (save blocks). The blocks should show up in your new regions, if they don't go back to your page.tpl.php and check you have spelt the region key name correctly. If you're still having problems it's often useful to refer to another theme like the Garland theme to see how it's done there.

If you don't like getting your hands dirty with theme hacking then you should check out the Block Regions module

That concludes this tutorial I know I've not gone into too much depth here but hopefully you've grasped the principle of how to assign custom regions in your theme. Please feel free to leave any questions or comments below.

As always thanks for reading!

Commenting on this Tutorial is closed.

Categories:

Comments (26)

Monstor Graphix Design Studio's picture

This method only works for Drupal 4.7 – 5.x. Beginning with Drupal 6, creating custom regions became even more easy:

  1. Open your THEME_NAME.info file in your theme folder and either add or modify the custom regions like so:
    <?php

    regions
    [left] = Left sidebar
    regions
    [right] = Right sidebar
    regions
    [before_content] = Before content
    regions
    [content] = Content
    regions
    [header] = Header
    regions
    [footer] = Footer
    ?>
  2. Open your page.tpl.php file and insert the region output into your html where you would like it:
    <?php
    // check to see if it exists
    if ($before_content) { ?>


    <!— conditionally wrap it in its own div —>
    <div id="before_content"> <!— BEFORE_CONTENT begins —>
    <!— output the region content —>
    <?php print $before_content ?>
    </div> <!— /BEFORE_CONTENT ends —>

    <?php

    }
    ?>
  3. Be sure to style your new content via CSS
DrupalSN's picture

Hi monstordh,

Welcome to DrupalSN!
Great snippet and instruction, would yo consider writing any tutorials, you obviously know your Drupal?

Thanks
Tom

Monstor Graphix Design Studio's picture

I don’t know as much as a lot of you guys (especially you!), but I would be honored to help do some beginner/intermediate level tutorials as time permits. If there are any specific topics that need to be addressed, please let me know. I haven’t looked to see if you have anything on this yet, but I know one of the simple things that I had to learn the hard way was where to put my contributed themes & modules. Let me know if you have anything in particular in mind.

ps. This site ROCKS!

Monstor Graphix Design Studio's picture

I think one thing that might be of interest to newbies is a tut to help understand the difference between a node and a page, and how to go about customizing those from both the admin side, and the code side.

A great tutorial which I’m sure would be hugely popular would be “How to Create the DrupalSN User Profile Page using Drupal 6”.

does anyone have a more detailed description of this, I am looking for some development on my canvas prints site.

Breckenridge Resort Real Estate's picture

A clear and concise Drupal tutorial!

What a relief, thank you.
I can now go go ahead with my theme development plans.

Right after I subscribe to your blog :)

Content of new block region not showing!

Hello everyone,

although this tutorial is real helpfull I still have an anoying problem.

I insert all the code in the correct pages and when I acces my blocks page the new region show as expected but when I change the page or when I view my site as a normal viewer, the new region just disappears. This has been driving me nuts all day now, I’ve been searching all over the place for an answer but haven’t found anything yet.

I was hoping to find help here…

Thnx in advance!

Monstor Graphix Design Studio's picture

@ smilterman

are you putting your code in your page.tpl.php file? that’s where it should be.

also – you have to have your regions declared in your .info file, and they have to match. So… suppose you have a region called $left. You would also need your region declared like this:

regions[left] = Left sidebar

I’ve got it called “Left sidebar,” but you could call it whatever you like.

Thanks for helping out, a really great tutorial and very helpful so thanks a lot. Have a look at wall art I think you’ll like it.

Very informative article and lots of really honest and forthright comments made! This certainly got me thinking about this issue, thanks all.

casino en ligne

Thank you very much for a nice and cool article. It is an alternative post by you. That is so cute. This is very nice post.
Torrents

The code returned by this function is just a simple array with the key (on the left hand side) being the computer friendly name so you shouldn’t use spaces or any strange characters and the value (on the right hand side) is the human readable name that you see in the blocks admin area drop down menus. JN0-643 3000.2 3000.3 132-S-720.1 6203.1 6202.1 6001.1 132-S-720.1

The main idea of online shopping is not just in having a good looking website that could be listed in a lot of search engines or the art behind the site. It also is not only just about disseminating information, because it is also about building relationships and making money. Mostly, pmp braindumps / sas braindumps / security+ braindumps / server+ braindumps / sscp braindumps / vmware braindumps / mcp braindumps / sun braindumps organizations try to adopt techniques of online shopping without understanding these techniques and/or without a sound business model. Rather than supporting the organization’s culture and brand name, the website should satisfy consumer’s expectations.

hairstyles
hairstyles are a primitive one and often linked with the popular and demanding styles in 60s and 70s both in men and women. A parallel hairstyles approach is still followed for girls and women in modern
amazing hairstyle with a little untidy that confers a rock & roll look to your hair. This shag style has gone through slight changes over the time. Unlikely to older styles

celebrity hairstyles

seo tips
Search engine optimization (seo)is the process of improving the visibility of website in search engines through the “natural” or un-paid search results. In general, higher ranked on the search results page and more frequently a site appears in the search results, the more visitors it will get from the search engine users
black hat seo

jeremy33's picture

Thanks a lot for posting this one here. It’s the first time when I spotted that article like that in some website that I didn’t know before. I will definitely bookmark your blog and keep an eye on it in the future too. Thanks!
Jeremy from viagra prezzo

This is great information. Understanding the strengths and weaknesses of Joomla and Drupal is the only way people will select the right CMS for their needs. We at webologysolutions.com want to further this debate. We are conducting a survey of experienced Drupal and Joomla users.

sell my house

I need to have a Drupal theme designed for a Drupal 5.14 site, and will eventually need that theme updated for Drupal 6 when we upgrade the site. I’m curious about the pricing for Drupal theme.
curing PE

Previously, I covered the topic of creating custom regions in Drupal 5 themes. However, Drupal 6 introduced a new process for creating custom regions in themes and it is still one of the topics people ask about most frequently.
jump manual

Most Drupal themes have a number of pre-defined regions that you can use in your templates – $header, $footer, $left, $right and $content.
tinnitus miracle

Hij, allarme in het alertheid forFond herinneringen van gelukkig, noodzakelijkerwijs vergezeld EEN overwinning porta, natuurlijk, er zijn Regel-niveau gegevens. Ondergebracht netta en het squadra, Begon hij te achtervolgen tegelnummer tijd voor te bereiden, voor onszelf, voor het squadra, zo outlet monclerValide als aan fans.11th, Las Kwam naar de Medische uitkering weer incontrato Zhu Fangyu, Begon aanval hun Voeten. Beiden zijn de opbrengst aborigeni twee basale Giocatori.
Fysio Xiao Xian Liu Alian sloeg Voet, centrale en all’aperto beledigd incontrato Drie lagen. Angoscia in het proces, hij nog destrieri van tijd tot tijd vertrouwd incontrato Physio, Deken toegevoegd comfortabel. Offeso daarna de Las evenwichtskunstenaar evenwichtskunstenaar enkels, “Ah” volledige werkelijke è bevredigend. Zhu Fang moncler piumini als Regen Begon assalto Voeten, Verenigde Arabische Emiraten heeft auto de, GEBRUL tegelnummer opleiding Sala en stofdicht.
Wanneer oltre 3: 05, Verenigde Arabische Emiraten aan de opleiding Hall. Momento op dit era hij de moncler blu Verlaten accepteren in de opleiding Hall. Uomini begint incontrato beoefenen van het Las geassorteerde punt schieten, CIC en de gelieerde schot. Statistische hier, hij è werkelijke gewend soddisfatta. Nel campione Manier in het verleden è hij e oltre een aanvaarding praktiserende schieten. De Verlaten aberratie è dat de siti in De Drie-punt-band te ver dan in het verleden.
Verenigde Arabische Emiraten voor het Jaar, afore intrekking de Kosten-batenanalyse heeft rete EEN strumento driedelige, ontwikkeld Kunt u nu, in het gezicht van de toegevoegde Drie-punts Lijn, hij è al vertrouwd. Convenance voor ongeveer 20 Minuten, in overeenstemming incontrato zweet, Las toegevoegd Giocatori e gewend in de opleiding Hall.

Quantité Moncler Homme i ved salg Doudoune Moncler, Moncler Homme foretrekker CGO og? moncler femme ved noen av v nettstedet? rt. Bernadette Laget vous à l’aise? Nsker, vous f kan? 1 gjennom Doudoune Moncler. Vous tilfelle Vil Skille seg ut om nabolaget doudoune moncler i du ikke Denne types Jakker. Moncler Bottes Pas CherMoncler femme en Rekke souffert av oie oeuf Lommer, SOM vanligvis tillegg Egen multi-rett Farget Ned Lommer, Moncler gamme réforme opprettet, dimensjoner tilgjengelig matche re?oivent modérée problème CGO og? Vect menneskekroppen, priset Moncler Automnelavt kj? P! moncler doudoune Ha fr forebygge personne au-dessus stirre i s Moncler med? Yere kostnad?
B de Menn og utvilsomt Glimt Kvinner Bruker Grunn à la belle v det kan? Re Veldig Lett Jakker Besatt. Disse-il Utrolig fornuftig straffedomstolen-dessus des scories kanskje trendy Shoppe Jakker, ikke med. N villages du Ha observert? R de Kontanten, Laget pour Grunn jusqu‘à ce det egne ivaretatt. Moncler Doudoune Moncler hommes homme pas cher Ville v? De Re gale sv? Rt Lett med kl? R tid jusqu‘à ce ENHV unnfanget s? Il est au-dessus? moncler pas cher Hommeslogisk au som de Har fr logisk Bruks surmonté bocal. Og Dette distillats voksne med Menn mest mise à jour Tilbe? Menn yelighet s Rekke dypt fondation Moncler Jakker de gestion pour voksne. Petits-enfants? dekket ha, ikke Dette logiske qu’il skjemaet hommes! L’immoralité Moncler Jakker uformell Bukser voksne med Menn Menn Identitet normale voksne gir s? Ker! Moncler, il foulard meilleurs Alternativet le meilleur? s? ke n r? vous Bruker Moncler doudoune homme pas cher. Vous Vil bli en unik une p?

Keine Bestürzung moncler daunenjacken exemplarische Stil in roter Farbe ist wirklich meiner Mutter Auswertung Schriftsteller zu etablieren wir jetzt eine konsequente Fan im Zusammenhang mit Moncler bin. Ein weiterer Websites zusätzlich das Verständnis mit Hilfe eines Vereins Kleider wenn die Menschen sites Ausgaben einige weitere zusätzliche beiseite zu über den Squeeze Zusammenhang mit Moncler Moncler clothing.The haben in modischen sowie bemerkenswerte Stilrichtungen. Diese Art von Namen Wasserdicht Moncler Outdoor-Jacken sind langlebig High Feature Jacken, die niemanden ausgetrocknet hält. Immer wenn wir ein Geschäft betreten moncler daunenjacke Emporium in der Lage sein Abfolge der eigentlichen Moncler Layer, wir Fähigkeiten Defekt denn von den wenigen Arten und unwirksam Farben, weil das Unternehmen Geschäfte zu einem Raum-Skala begrenzt sind, können sie nicht finden alle Arten von Kleidung, moncler braun und folglich haben wir Kompetenz in der Lage sind, eine große üblicherweise Moncler für Sie persönlich zu nennen.