Community & Support

best way to get clean xhtml-output?

Hi, I am new here - just starting to find out how to template for Drupal.

My question: How do I get the cleanest possible code out of Drupal?

I noticed that Drupal has the tendency to add spans everywhere, use a a lot classes per div and have a different div for every element + for every group of elements. This is of course a result of the Drupal philosophy about blocks and regions and in a way it gives one a lot of handles to style the result with css.

But idealy I would like to be able to have the contents of (CCK) fields with as little formatting as possible (just filtered html with or without a little tinymce) and give that a place in my template.

Is that possible? Do I need to code directly in php for that or can I still use phptemplate, or...?

TIA!

Comments

Without writing a book its

Without writing a book its simply a matter of theming out what you don't want. Drupal is by and large a top down approach, so you have to remove what you don't want, and many modules work like that - CCK, Views, Panels to name a few.

You will need to know some PHP - because a lot of this is most effectively dealt with using theme function overrides and preprocess functions. Templates will take you a long long way, but in the end its useful to at least know some PHP.

The good news is that theres almost always a way to get this done pretty easily, although theres some hard bits like forms where you need to get serious with hook_form_alter...

I don't mind getting my hands dirty

Thanks for the answer!

So the direction you are pointing me is:
- study the theme function overrides and hooks (or is that the same?)
- study the preprocess functions

Does this mean that:
- these functions are used to alter the output of Drupal?
- these functions can be used to present/sculpt the data the way you want it (which I would prefer)?

Are there any templates that do just that so that I can study them?
And speaking about books: which book(s) would you advice - given the goal I want to achieve?

Note: Some time ago one of the people working on webkit stated that the use of too many classes and too many possibilities to target and alter those classes could in theory bring a browser to its knees. To roughly paraphrase him: the combination of certain selectors plus javascript gives you a lot of rope to hang yourself with. There are a lot of things I really like about Drupal, but I am afraid the way content is sculpted is not one of them.

Thanks again!

...

So the direction you are pointing me is...

Yes, 100% go in this direction, and study the entire theme system, templates are very important and are scattered through the core modules and contrib modules such as Views.

Does this mean that...

Yes, correct again. On the money. Mostly you call them from template.php but I often abstract function overrides to theme.functions.inc and call it once from template.php

Are there any templates that do just that so that I can study them...

Any advanced theme like Genesis or Zen is worthy of intence study, both themes pull of some real trickery behind the scenes. Acquia Slate or Acquia Marina is worth looking at as well, in fact I would download a whole lot of contrib themes and deconstruct them.

Books - Front End Drupal and Pro Drupal Development for starters, then there's the Packt book on jQuery and JavaScript in Drupal.

Not zen, not looking deep into code yet...

I would suggest that trying to understand zen theme is actually not the easiest start. It is power-tools for people who know theming, but there is just too much cleverness in it to grok in one reading.
To learn Drupal theming, start with how it currently works, and only then figure out if and why you need the tricky extras.

FIRST Look at the /modules/system/*.tpl.php for the very basic themes that Drupal core starts with. You can make those even simpler if you think it's a win (?) but zen, views and the rest should come after looking at the 'naked' themes first.

...

Yeah, I totally agree, http://drupal.org/node/190815 has a great guide to default templates and the D6 theme guide is pretty darn good once you grok it.