Sustainable Markup: How to be a themer in Drupal.

Colleen Carroll's picture

I’ve been thinking a lot lately about how we could be doing things better and not just in the Drupal way. But Drupal is what I’ve decided to focus on first. I’ve been wracking my brain over how we can improve our process, how we can build more efficient sites faster and better for the client, and how we can give ourselves a tool kit.  And then it hit me, we’ve been approaching designs in Drupal all wrong this last year. Don’t get me wrong, we didn’t do anything "wrong", we just didn’t make it easier on ourselves.

We’ve been a Drupal shop for over a year now and we are definitely still maturing our Drupal approach. We are still cutting our teeth, so to speak. And I think that we may be reinventing the wheel all too often.

Of course, the hilarious piece of this whole epiphany of mine is that it is completely obvious. Every day I/we use the term “theme” or “theming” and we even call ourselves themers, yet we don’t theme, we skin. We even talk about themes that other people built in Drupal, and I look at Zen and Garland all day long – and still it didn’t hit me.

A “theme” as defined in Merriam Webster’s Dictionary is “a subject or topic of discourse or of artistic representation”. And subjects and topics shouldn’t break down, and if they do, it will break off into a whole new topic or representation and generally isn’t shoe horned into the original representation.

In Drupal, “to theme” means to abstract or override the look and feel. But really in Drupal, to theme means to create a tool kit, to create a design that is an overlay to your core functionality, and is sustainable. Yep, it’s everybody’s favorite word – “sustainable”. The theme needs to be something that lives and grows with ever evolving functionality.

So let’s get back to our current method of “skinning” instead of theming.  I’m finding that we are more reactionary in our CSS/theming approach on a site than tactical, and I suspect lots of other people are too. We're missing a solid formula for our themes. This is what Zen does, it’s what Garland does, and it’s what any good themer who contributes a theme back to Drupal does.

The first reason why I think developers tend to lean away from creating a real theme or tool kit is not always an intentional one, but is because most of us come from a background of building our own markup and CSS, never having it dictated to us.

The hardest challenge for me in Drupal was to stop trying to rewrite it, and no, I don’t mean hacking core. When I started my first theme or “skin job” ;), the first thing I wanted to do was rename every class and id to my own preferred name. It was so bad that I renamed our version of Zen’s “page” container to “pagewrapper”. That should have been my first warning sign. Thankfully, over time, I began to trust Drupal and learn to skin (notice I didn’t say theme) Drupal, use the theme templates as is, and not fiddle with the markup, classes, or structure, but to instead write my CSS based on what Drupal gave me. In return it made me a much more skilled web developer. When you have to work with what the CMS (Drupal) is giving you, you rely on your wit and start to learn some pretty nice CSS tricks. In case anyone’s wondering how you would ever do such a thing…it's called Firebug, and it is awesome. No web developer or themer should be working without Firebug or the Web Developer toolbar.

For those who don’t know, at Palantir we often collaborate with design firms to build a site, which is why it is even more important that we wrap our head around this idea of a theme. An extensible design, a design tool kit that continues to evolve, without the help of a HTML/CSS developer is the ultimate goal; a design or theme that stays intact as you add new content and new layouts to your site.

Two specific examples of this in Drupal are Views and Menus. Often, we style view blocks, menu blocks, and view pages once we display them, and man, does it always need it. My question is…can’t we figure out an approach that always lets you drop a menu in a region and have it look acceptable? Or add a new table view to the site and have it follow the design thread?

When you look at Zen’s stylesheets you’ll notice that the CSS has been built for 1 column, 2 column, 3 column, and blocks in any of those regions. You can’t just design for what appears, you have to design for all outcomes, and especially one that involves a view block in an area where you never thought it would go.

Do I have a solution? Well, maybe not a solution but perhaps an approach to get us there quicker. After working with the latest version of Zen for D6 (a fairly well-developed toolkit), it has become incredibly apparent to me that we need to plan better for the site and all the legs, arms, and other appendages that could occur. Granted, we're often working within a limited design, but I wonder why we don't style along with our semantic markup. We're pretty good about laying out semantic sites, but are we styling them semantically? Or are we seeing a title that should be purple on this page and blue on another? In other words, H1s should serve a specific function and they should always be distinct. And H2s, well, they should also be somewhat prominent and not 10px in font size – because is that going to hold up in your design, and be extensible? Drupal structures items with H2s all the time…for good reason - we can’t abandon that reasoning, we need to embrace it and theme/design for it.

I think that the reason for this missed opportunity is that too often as web developers we are focused on designing a site and not a framework. If we were designing a theme to contribute back for any site, we might be forced to address some of these semantic structure decisions.

Aside from shifting our focus, another approach that we at Palantir recently adopted is the use of an interactive prototype. This prototype is essentially a Drupal site, with FPO content, and all the content types needed by the client, styled in Zen, with a little bit of light wireframing design, i.e., grey outlines. So the first phase of our project is finally asking the client to nail down what they want on their site and how they want users to enter that data and retrieve that data. We then let Drupal do the work, and let the designers theme the output, NOT vice versa. How does this help? If you have the ability to do this interactive prototype before designs are even created, you can then use the wireframed prototype in Drupal as a stepping stone for the design. The beauty of Zen is that even with its scaled back CSS, it maintains a framework and upholds semantic markup.

Like I said, for some this might be obvious, but for others who’ve been missing the point like me, this brings a whole new focus to my approach to theming for Drupal. So I hope to embrace that role as themer and build toolkit into every site I work on. No more skinning, time to theme.

themes vs skins

Relying on CSS whenever possible is a good strategy. Overriding Drupal output with theme functions can come back to bite you, especially when it comes to maintenance/upgrades.

Thanks for an interesting

Thanks for an interesting read. Drupal's vocabulary and the resulting semantics have provided/will provide debating points for a while (e.g. http://lizasabater.com/drupal_vs_the_blog_meme).

Very interesting article,

Very interesting article, thanks for writing it. Theming in Drupal seems to be a hot topic today. Just earlier I wrote an article on some trends I've been seeing recently in the Drupal community regarding 'conceding to the Drupal way'.

You made an interesting point here:

"you have to design for all outcomes"

For the Zen theme, absolutely. The Zen theme is intended to provide a starting point for 'themers' to construct their individual themes with.

But what about the typical Drupal site that doesn't need every piece of functionality that Drupal offers? If they use a theme that's had so much emphasis put on pleasing all points, they're left with a partial theme that may very well be difficult to use for the site's particular purpose.

I think we can all agree that Drupal's general UI needs some serious overhauling, and I'm afraid too much of that UI neglect is pouring over into retail themes.

Nick

Same path

I followed the same path when I started working with Drupal (4.7 at that time). I could override anything and use my own markup, and that was something other CMSes didn't offer. Not with the same ease at least. So at first it was theme_feast, I hacked happily.
Then I discovered that was indeed a lot of work. And that modules and APIs may change with every new major release, so your overrides may well break horribly and give you even more work when you upgrade.
Now I use the default html output too and only override when I have to.

I have started a small video

I have started a small video to train css coders to adopt Drupal quicker. I think where you are heading with your article is standardizing how we theme Drupal sites. For sites that DO NOT have custom blocks, views, form_alter()'s, tons of path based template (tpl) files, one CSS stylesheet from one site should be able to be put into another and not break the site.

Themers (skinners) should only have to work with CSS and maybe some HTML from time to time. BUT, we all know that is not the case with medium to large size Drupal sites. I haven't worked on a Drupal site in over a year where I didn't need to add some theme function override or write my own. Or custom blocks / views. So standardizing is not so easy.

For me, the trick is to get CSS Coders to realize what Drupal is giving them from the get go. Blocks, Views, and any customizations in the theme layer will (and should) have their own classes and id's. The CSS coder should map his xHTML output to Drupal - that is not always easy to get them to understand though. But, it is vital if you are working with big clients that need more work done later with a different CSS coder.

My hope is that themers, css coders, skinners, whatever we want to call them, will begin to work together on some form of standard so all of us could jump into a site and begin working on CSS without no question to what is going on or why this was done this way etc. A bit vague and a large challenge but I do believe it is doable.

Thanks for the article.

Thanks for your input Elvis.

Thanks for your input Elvis. And I agree with you that this is an enormous challenge and it is something that gets increasingly more difficult the larger the site and the more the design strays from a formula.

I do think, however, that by relying more on the Drupal admin to tweak content types (display filters for example), and by also leaving the page and node tpl files as pristine as possible, you can get closer to this tool kit model. For example, I try to leave as much content from a view as an unordered list, and I arrange the fields in the view admin in the order I want them to appear on the page. Then by using the theming wizard in D5 for views I may add an extra div, but I do not remove the ability to add labels from the admin, or alter the classes given to me by the theming wizard. Or in some other cases I find that I may not even need that tpl file anymore. And what this means is that the view is now more flexible, even from a design perspective.

We've begun to "assemble" or manipulate fields on a given node type or view by using a preprocess function - something that was introduced in D6, but my colleague back ported for us to use in D5.

Essentially my goal, and the goal I think everyone should strive for is to limit the amount of overrides and tweaking done on tpl files on a Drupal site. Every site will need some, but I think that with some structure and some well thought out CSS you can theme what Drupal gives you.

Yes, I agree with the goal.

Yes, I agree with the goal. But Drupal isn't "that" flexible quite yet. For example, clients are picky (and have the right too :)) from time to time and what node/add/x forms to look a certain way, or the admin/content/node. To do this you will need to use form_alters() or do some custom hacking in the template.php file.

When Drupal or a module eliminates this, then we will cross a huge bridge and get much closer to this idea of standardizing.

Thanks for the preprocess tip for D5, very nice.

Reset the tag soup

I certainly understand where you're coming from in terms of just using the out of the box classes and ids with CSS and not getting carried away with theme function overrides.

The problem is that most themeable items come with simply far too much bloated markup, just to accommodate everyone who missed the "cascading" in CSS.

And this is not a problem of a particular theme...it's the default themed module output, being overly cautious to provide multiple classes and ids to the last multiple nested div'ed-up sub-element.

Can you style it any way you want? Sure, but looking at the actual HTML output can be pretty ugly.

For a Drupal development shop, it could be handy to have a standard .inc file with the most used module theme override functions that goes into every new project theme, reducing the markup.

Just the markup, please

On every large scale site I've themed, I completely clobbered any CSS contributed via drupal_add_css that comes from core or third party modules. Otherwise you wind up writing a lot of rules that simply override undesired behavior, rather than writing rules that express only what you want.

I don't know how someone theming a site can be responsive to detail-oriented clients and not have to learn at least a little about Drupal's templating mechanism and a little about programming to accomplish their goals. Perhaps if there is both extremely tight specification and coordination between developers and themers, and a nice bright line between the two roles.

The Drupal provided markup is not always in line with what clients want, in ways that CSS alone cannot account for without difficulty, and it is a rare day that I don't have to override this or that theme function to achieve a client requirement.

Making Drupal easier to theme

Interesting read. Instead of teaching themers (skinners?) how to wrap their brain around Drupal, we should try to define what we can do to make theming Drupal more natural for themers. If you were able to change 3 theme related things in future versions of Drupal, what would it be?

Dries, thank you for

Dries, thank you for commenting! I have to say, I want to think real long and hard about your question. There are so many things that I think I as a themer do repetitively on every site in Drupal. Steps that I always take that don't need to be taken on every site, things that could maybe become automagic.

Stay tuned for a more thoughtful answer :)

Top 3 Drupal Theming Enhancements

First of all, great read and a fantastic site!! A well deserved congratulations to the entire team at Palantir!

Regarding Dries question on theming, if you don't mind me throwing my comments here are my top 3:

1. Additional module development to build a CSS baseline

Drupal core and non-core modules need to make a concerted effort to establish an acceptable output and CSS for their content types. Too often I activate a module only to find the output is not great and appears to be an afterthought. Not to pick on any one module, but the "event" module produces a node that starts with this:

Start: 07/30/2008 - 19:57
End: 08/01/2008 - 19:57
Timezone: Etc/GMT

The IDs and classes are there to edit the CSS, but we ought to get a nice looking standard event node together in node-event.tpl.php and CSS elements before we release it. (perhaps with a background calendar graphic for the date, etc.)

2. Allow users to configure the output within the administration

For each content type, we need to be able to customize fields output and display - including whether or not to appear. This avoids having to code PHP to edit the formatting of the content type.

For example, I would really like to go into admin/content/types/event/fields and select to have a long, short, or custom date type for the start/end date within each event (kind of like what the views module does).

This way, I can choose "July 30th, 2008" or just "7/30".

AND I would like to select if it appears in the output at all. Many of the events I have created don't have a specific start time, and are an all day event.

We would need to do this section and formatting for all the different types of outputs - full node, teaser, etc.

3. Make #1 and #2 easy

3a. Leverage a common set of guidelines for CSS development and naming within Drupal. This is probably an area that was purposely avoided in the past - but there is significant value with a naming convention so that the CSS DIVS across the different modules are consistent, etc.

3b. Continue to leverage rich web framework like JQuery
I read a post on drupal.org on usability that talked about how important drag and drop is to users. I agree and think this is why we are seeing such a push for intuitive controls. We need lots more of this in the future and Drupal v6 started down this path.

___

All in all, I really enjoy the power and flexibility of Drupal. That said, we collectively need to continue to bridge the gap and drive towards better theming capabilities.

Hey Guy, thanks for beating

Hey Guy, thanks for beating me to the punch on this one. I too have been thinking the same things. I always come back to Views and think, man, why don't we have the same options for field formats in CCK? Here at Palantir we have built some fancy field formatters, but wouldn't it be great if you didn't have to download 20 different formatter modules, but rather download one "suite" of custom formatters?

CCK address is similar to what you described with the event module. You cannot format each individual field's display from the display settings admin for the content type. This of course is not a comment on the CCK module, but a comment on being consistent about creating add on modules to CCK.

The thing that I would love to see the most in future versions of Drupal is something similar to what Panels gives you - Configurable layouts. How great would it be if you could rearrange some HTML from a drag and drop screen. And even add in some extra unique classes or IDs.

Then the responsibility shifts back the themers to really create a theme based off of this "flexible" layout. More field formatting and visibility settings need to be added to Drupal, and/or Drupal tier 1 modules, like cck, views, etc.

The goal is to get rid of the themer next right?

Ha! As long as there are

Ha! As long as there are customers, there will be themers!! :)

I believe that the 80/20 rule applies and we should get 80% of the way there with great output from drupal, and tweak the remaining piece.

Thanks for your post, and I look forward to seeing some more great design from Palantir in the future.

Best,
Guy

Contributed modules not giving many favors...

So... reading and understanding posts and comments like those on this page would have helped me out a lot when we were cutting our teeth on Ubercart.

We've consistently heard how hard it is to theme various parts of our output, though many people are doing it and doing it well. I'm not sure Ubercart is unique in this aspect... I just would love to have a checklist of things to specifically do and not do when generating the HTML output and .css files in modules I develop so they don't annoy our themers. We try to imitate core as much as possible, but theming practices still elude me.

We've had some good feedback from folks in the forums, and I'm always trying to learn about the design side as much as I'm not a designer. However, I'd love to be directed to any resources you might recommend for Drupal module developers wanting to do things right by our themers. : )