I'm new to theming, so please excuse my ignorance.

I'm developing a newsletter site with a content type "article," which I created with CCK. I've written a file, node-article.tpl.php, that themes my article pages. It works, but it contains some awful kludges and I'd like advice on cleaning those up.

kludge #1: In node-article.tpl.php I can output the pre-determined fields like "title" with print $title; . So far so good. But when I want to output one of the fields that I've created especially for content type "article," I do this: print $field_subtitle[0][view]; . It works fine, but surely that's not the best way to output that field! Please tell me how it should be done.

kludge #2: When I go to output the "body" field, I run into problems. If I do this print $content I get the body PLUS all the fields I created for content type "article" including "subtitle," "byline," etc. But I just want the body field! After some time looking at print_r output for $content, I came up with this mega-kludge:

$temp = array_values($node->content[body]); $temp2 = $temp[1]; print($temp2);

Now, surely that's not the best way of doing this. Any advice?

Thanks in advance.

Comments

el_rob’s picture

Have a look at the "Contemplate Module"... http://drupal.org/project/contemplate

This might help you.

Cheers!
Rob

nicolash’s picture

AFAIK that module only puts in the database what you otherwise would put in a tpl file, no? You still deal with field_foo[0]['value'] types, I think. Personally I'd rather have that in a file, rather than in the database for migration and versioning purposes...

ourbrisbane.com

nicolash’s picture

It feels very messy. One way to keep your final tpl files clean with straightforward variables is to do some pre-processing in the template.php file...which acts as the interface between logic and html markup. Have a look in the zen theme for example, which is almost like a tutorial.

In the _phptemplate_variables function you would convert it into something like

$vars['subtitle'] = $field_subtitle[0][view];

which you could then use in the tpl file as simply $subtitle.

If you would have a number of subtitles (multiple fields), you'd probably loop through the array and put it all into one variable with markup (preferably via a theming function), still keeping your tpl file as clean as possible.

This is really just shifting what you described into a different area, so I'd be really interested in any "best practice" responses to this, but I thought this is better than "subscribe" :)

ourbrisbane.com