Rendering each page for each content type differently

evilgenius - July 1, 2008 - 19:23

Say I have a content type 'News' and I want all the pages on that particular content type to render page differently from other content/node types.

I have assigned all content types on my site to their particular URL - mysite.com/contenttype/*title. I manage to get page-news.tpl.php working but the problem with this is that the changes only occurs on mysite.com/news and not on the content view. When viewing the node already (through mysite.com/news/title) the page returns to its default page.tpl.php template. I want to render the page of mysite.com/news/title the same way as mysite.com/news. I don't know how to accomplish this.

Note that it's not the node I want to customize here, its the whole page where that node type is being displayed.

subscribe

JoepH - July 1, 2008 - 19:49

Good question!

...

mooffie - July 1, 2008 - 20:56

See http://drupal.org/node/256302 (that discussion is big, but the answer is about three lines of code).

Wow... I myself is a TOTAL

evilgenius - July 2, 2008 - 06:29

Wow... I myself is a TOTAL newbie and this guy is claiming he's a newbie too but I can't seem to absorb all the php stuff in there. 'Three lines of code'? It really sound so simple but I don't know which/where to put the what....

I wish there is an easier way around this like creating 'page-nodetype/*title.tpl.php' or something like that.

Sorry I'm just a newbie really trying hard my way around this stuff.

...

mooffie - July 2, 2008 - 08:29

Put this in your 'template.php'. Then clear your Drupal cache. Then you'll have support for 'page-nodetype.tpl.php'. We're talking about Drupal 6.

Incidentally, note that Drupal adds a 'node-type-nodetype' CSS class to page.tpl.php's BODY tag.

Thanks so much

evilgenius - July 2, 2008 - 17:02

Thanks so much mooffie!!!

The problem is that I already have the function phptemplate_preprocess_page() in my current theme template.php with existing variables:

/**
* Override or insert PHPTemplate variables into the templates.
*/
function phptemplate_preprocess_page(&$vars) {
  $vars['tabs2'] = menu_secondary_local_tasks();

  // Hook into color.module
  if (module_exists('color')) {
    _color_page_alter($vars);
  }
}

I'm not even really sure what it does exactly. I'm using the Framework theme btw.

... But still I tried inserting this code elsewhere in the template as per your instruction:

function phptemplate_preprocess_page(&$vars) {
  // are we on a page that displays ONE node?
  if ($vars['node']->type){
    $vars['template_file'] = 'page-' . $vars['node']->type;
  }
}

and it returned me a site error (as expected):
FATAL ERROR: Cannot redeclare phptemplate_preprocess_page() (previously declared)

So what I did was insert the new variables to the existing function and it worked like a charm! This is my new phptemplate_preprocess_page():

function phptemplate_preprocess_page(&$vars) {
  $vars['tabs2'] = menu_secondary_local_tasks();

  // Hook into color.module
  if (module_exists('color')) {
    _color_page_alter($vars);
  }
    // are we on a page that displays ONE node?
  if ($vars['node']->type){
    $vars['template_file'] = 'page-' . $vars['node']->type;
  }
 
}

So my question is... being a total newbie and all - Is that the proper way of inserting it to my existing phptemplate_preprocess_page() function? You know technically speaking. Though it did work, I just want to make sure that what I did is correct and I won't run into some problems in the future.

Thanks again.

...

mooffie - July 2, 2008 - 17:52

Is that the proper way of inserting it to my existing phptemplate_preprocess_page() function?

It's ok.

Or you could name that function XYZ_preprocess_page, where XYZ is the name of your theme.

I just want to make sure that what I did is correct

If you don't want to alter your designer theme files (because then you'll have to update them manually when the designer changes them), then you can create a theme which is inherited from that designer theme. You'll have a 'template.php' of your own.

OK will do just that! Thank

evilgenius - July 2, 2008 - 18:43

OK will do just that!

Thank you so much mooffie for everything. This is like the 3rd time you have helped me.

P.S: You going to Drupalcon next month?

...

mooffie - July 2, 2008 - 20:54

It's 4th, a moment ago I answered another :)

(No, I'm not going.)

 
 

Drupal is a registered trademark of Dries Buytaert.