Theming in Drupal has been complicated and difficult, particularly when approaching the problem of websites with multiple layouts. We've all seen sites with dozens of tpls, with code written into templates, with mile-long preprocess functions covering multiple possibilities… It can be messy!
But it doesn't have to be this way. The addition of just a few modules, and a solid base theme, allow a site's layout to be configured without writing a single line of code. This was the topic of Omega: From Download to Layout in 45 Minutes, my presentation at BADCamp. This post will cover just one part of that, creating a layout for one of the wireframes without writing a single line of code.
Dev Site Setup
We'll make the assumption that you have a dev site set up, with the appropriate content types and some demo content to support this site, along with a few views and utility blocks. You have also installed the Omega base theme and created a new sub-theme, titled Epsilon.
The site is laid out using 960gs, a popular grid system. For more information on 960gs, visit their site.
Here's a view of the section/content list page wireframe.
An alternate view of this layout clearly shows the columns widths used for layout on the grid.
The core of layouts in Omega lies in the Zone and region configration tab. Regions should be familiar to us from block admin - that's been around for a couple Drupal versions. Regions are where you put your content and blocks. But what are Zones and Sections?
Think of them as larger containers into which the zones are placed, in a hierarchy. Sections are the largest, and can have one or more zones inside them. Zones are next, and can have one or more regions inside them. Finally come regions, this is where the grid is really laid out, as Omega makes them quite easy to size by columns. These serve to wrap and contain your HTML, allowing for common styling and easy layout choices.
In this third view of our wireframe, we clearly see three different sections.
Let's start by looking at the header section in detail. The wireframe really only contains one zone, with two regions: logo and menu. Opening the Header Section configuration menu in Omega, on the Zone and region configuration tab, we see that this section actually has 4 zones, with a total of 6 regions in it – clearly too many for what we need, but a nice example of how versatile Omega is.
Omega allows us to move zones from one section to another, or even to disable them altogether. We do this by opening each zone's fieldset, then the configuration fieldset within that, and setting Section to – None –.
In this case, we'll start by doing that for User Zone and Header Zone.
Next, we set the width of the Branding Region in the Branding Zone to 4 columns. This is where we will put our logo, which if you recall was 4 columns wide.
After that, we go to the Menu Region in the Menu Zone, and set the width to 8 columns, as laid out in our wireframes. Weighting in Omega works just like it does anywhere else in Drupal – the higher the weight, the later the item renders in the process, so we set the weight to 5 to push the Menu Region after the Branding Region. And, again, Omega allows us to move a region from one zone to another, so we move the Menu Region to the Branding Zone. This will stack the Branding and Menu regions horizontally without any additional CSS. Doing this leaves the Menu Zone without any regions, so as a last housekeeping item we set the Section for Menu Zone to – None –.
The Content Section comes with three zones by default. I'm going to leave the Preface and Postscript Zones alone for now, and work in the Content Zone of the Content Section. Yes, there's a Content Section, and a Content Zone, and guess what? There's a Content Region, too. Naming things in Drupal is not an exact science.
We're going to focus our attention for now on the Content Zone.
In the Sidebar First region, I'm going to set the width to 4 columns, and set the weight to 5. Again, a higher weight will push the item later in the rendering process - in this case, after the Content Region.
I'll set the width of the Content Region to 8 columns, and leave the weight alone.
Finally, I'll set the Zone of the Sidebar Second region to – None –, which will remove it from the Content Zone altogether.
Lastly, in the Footer Zone of the Footer Section, I'm going to set the width of the Footer First region to 8 columns, and the width of the Footer Second region to 4 columns. Since these two regions are already in the same zone, they will line up horizontally automatically, so there's no need to move them around from one region to another.
Save and view the page. Here's what a default Omega sub-theme looks like, out of the box.
And here's what Epsilon looks like now.
All that – and no coding!
Delta and Context
The problem of applying different theme settings – layouts – to different pages remains, of course. That's where the delta and context modules come in. For a more thorough explanation of how they work, see the slides from the full presentation at our Slideshare.