Long enough subject :-)

Yes, I am needing to override what is going on in page.tpl.php for a specific node content-type and I'm wondering the "drupal best practice" way of going about it.

My needs extend outside what happens within node.tpl.php, (regards the page title etc.. want to move it within the content area of this content type)

I don't really want to add hardcoded if-thens into my page template.. but page-node-[contenttype].tpl.php i guess is not supported. Or is there a different pattern for that?

Thanks,
Reuben

Comments

silverwing’s picture

create a page-contenttype.tpl.php file.

in the original page.tpl.php add this line to the top (changing the necessary bits)

<?php if ($node->type == 'contenttype') {include 'page-contenttype.tpl.php';  return;  } ?>

~silverwing

_____________________________________________
MisguidedThoughts | showcaseCMS

dvessel’s picture

Not exactly a "best practice" approach. Add another template naming suggestion instead:

function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page' && isset($vars['node'])) {
    $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  }
  return $vars;
}

Now you'll have your "page-[contentType].tpl.php" to work with.

joon park
www.dvessel.com

arla’s picture

For Drupal 7, do this instead:

function THEME_preprocess_page(&$variables) {
  if (isset($variables['node'])) {
    $variables['theme_hook_suggestions'][] = 'page__'. str_replace('_', '--', $variables['node']->type);
  }
}

Put it in template.php in your theme folder. And clear cache.

amirtaiar’s picture

I am using fusion so I used the templete.php of the core theme and put this lines.
then, in my sub theme I have create a page--book.tpl.php but it couldn't read it...

- Empty cache/
- Try to copy templete.php to the sub theme and chane all MYTHEME_...

Managing Partner at Reasonat

frank ralf’s picture

Thanks!

davidgtuttle’s picture

I believe this only works if you name the template file - page--[content-type].tpl.php. So... you need two hyphens then the content type machine name.

cloneofsnake’s picture

I'm new to Drupal, but read this on the Pro Drupal Development book. Not sure if this will help, may be some of u experts can clarify for me too.

The node.tpl.php file is the generic template that handles the view of all nodes. What if you want a different template for blogs than forum posts? How can you make node templates for a specific node type rather than just a generic catch-all template file?

Simply clone node.tpl.php and rename it to node-nodetype.tpl.php is enough for PHPTemplate to choose this template over the generic one.

Any node type you create via Administer > Content management > Content types can have a corresponding node template file in the same fashion.

silverwing’s picture

you can create any node-contenttype.tpl.php for any content type you create.

But the original poster wanted a specific page-contenttype to style the whole page differently. (node-contenttype.tpl.php will just alter the $content part - the title, text, add comment, terms, etc. page-whatever alters the look of the whole page.

In Drupal 6, I think you can create a page-contenttype.tpl.php in your theme and the theme engine picks it up.

In Drupal 5 you have to have a workaround for it.

~silverwing

_____________________________________________
MisguidedThoughts | showcaseCMS

MrBigD’s picture

In 5.7, garland automatically reads page-contenttype.tpl.php but bluemarine does not. In fact I was unable to force it to do so with the following code: (node type is type 'detail')

<?php
if ($node->type == 'detail' ) {
print $node->type;
include page-detail.tpl.php;
return; }

Still will not include the page-detail file - Any ideas?

dvessel’s picture

It will not work for 5 or 6. You have to add those suggestions yourself.

For Drupal 5, see the example I posted above.

For Drupal 6, you have to use preprocess functions to do the same.

function phptemplate_preprocess_page(&$vars) {

  // $vars['node'] is available when the page is focused on a node. i.e., example.com/node/123
  if (isset($vars['node'])) {
    // Add template naming suggestion. It should alway use hyphens.
    // If node type is "custom_news", it will pickup "page-custom-news.tpl.php".
    $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  }

}

More can be read in the handbook page for 6.

joon park
www.dvessel.com

jjma’s picture

Hello,

I've added you example to the top of my page.tpl.php:

function _phptemplate_variables($hook, $vars) {
  if ($hook == 'page' && isset($vars['node'])) {
    $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  }
  return $vars;
}

but the new page will not take. In this case I called the page page-client-page.tpl.php (taken from the client page content type I created).

Renaming the default page to page-node-76.tpl.php works but only on the one page and not the others made by the same content type.

Any suggestions?

Jon

smatthews1’s picture

I believe that this is supposed to go in the template.php in your theme directory... Which I did try and still nothing.

I believe we shouldn't even have to do that since this is supposed to be a basic feature of Drupal

jjma’s picture

I can't get it to work by adding a page template based on the content type in my themem directory.

i.e my content type is called x
My page template is named page-node-x.tpl.php

Drupal 6 doesn't pick it up......

shogikishi’s picture

I'm trying to do the same thing in Drupal 6 - create a url specific theme. It is for a basic form page. However page-my-url.tpl.php is not being picked up in the theme directory.

Anonymous’s picture

That works in 6, but doesn't help you override a specific node type.

Melot’s picture

Just add this to your template.php in Drupal 6 to enable content type-specific page.tpl.php:

<?php
function phptemplate_preprocess_page(&$vars) {
    $vars['template_files'][] = 'page-' . $vars['node']->type;
}
?>
ballboy’s picture

Groovy, I've tried a bunch of similar snippets but this last one is the only one that worked for me (and is the simplest of them all)...

ressa’s picture

If there already is a function called phptemplate_preprocess_page in your template.php you will get a WSOD, in which case you should just insert the code part in the already existing function.