http://drupal.org/node/46006 seems to say that in Drupal 5 you can change the page display for different node types simply by creating a new page.tpl.php file for that node type - e.g. page-story.tpl.php.

However, it's not working for me - I just get the same old page.tpl.php every time.

Has anyone else had this problem? Can anyone point me to a solution?

Lucy

Comments

jwilde’s picture

change page-story.tpl.php to node-story.tpl.php jim

lucyconnuk’s picture

Thanks, but I'm trying to change the display of the page, not just the node within the page.

In previous versions of Drupal you had edit template.php, but this post seems to imply it's no longer necessary. But maybe that's not correct?

Lucy C

yurg’s picture

Sorry for stupid question, but did you put any changes inside page-story.tpl.php file after creation?
Let's check your way (tell me, if I'm wrong in reproducing of your steps):
1. You've created new page-story.tpl.php and put into newly created file some additional string to inshure it works and to separate story-tpl from page.tpl. Adding simple "hello, I'm a story" after last </div> tag of page-story.tpl.php can be enought to test.
2. You've upload newly created file into right place (theme folder of your site)
3. You've created new Story (via www.yoursite.tld/node/add/story)
4. You've seen, that nothing has changed in new story, and magic "hello, I'm a story" string does not appear after story content.
Am I right in reproducion, or may be something missed?

lucyconnuk’s picture

Yep, that's exactly what I've done... and nothing's happening!

I just changed page-story.tpl.php to add some text before the title:

        <h1 class="title">*** STORY *** <?php print $title ?></h1>

I am writing a theme called accommodation (which at the moment is more or less just a copy of bluemarine), and it's in
sites/all/themes/accommodation.

The site I'm working on is here if you want to have a look:
http://www.finsta.co.uk/ms

(This is a test site so you can post stuff if you want, but it won't be there very long.)

Lucy C

yurg’s picture

Yep, according to your explanation, you really need node-story.tpl.php, rather, than page-story.tpl.php, and jwilde, suggested this in 1-st comment, was right. In fact, if you need to change look of "content" area (inside and close of content and comments), you can do it via node-story.tpl.php. In this case, all other "theme-related" stuff around Story (logo, colors, fonts, etc.) will stay untoucheble. But, if you want to change theme, applied to selected content-type (story), you need to deal with old good template.php, as dvessel suggests.

dvessel’s picture

All the alternate template names are done by passing "suggestions' to _phptemplate_callback for theme functions that implement them.

Look at phptemplate_page inside phptemplate.engine to see what it understands. It doesn't understand node types but you can change that by adding your own suggestion.

Inside template.php you have to add this if it's not in there already.


function phptemplate_variables($hook, $vars) {
  // Node is passed to the node when is rendered as a full page.
  if ($hook == 'page' && isset($vars['node'])) {
    $vars['template_file'] = 'page-'. $vars['node']->type;
  }
  
  return $vars;
}

now "page-story.tpl.php" or any node type should trigger it only when the node is rendering as a full page. (not teaser list).

$vars['template_files'] (plural form) is also valid but then you can pass it an array for it the scan through. The later ones added would be checked first.

lucyconnuk’s picture

Thanks dvessel, I tried it, but it still isn't working.

I had to add an extra ) after $vars['node']) , but even then it's not working for me.

Lucy C

yurg’s picture

Another piece of drupal docs, founded here: http://drupal.org/node/46027 may consist right explanation, but may not. It's Drupal, and where is only two ways: it's work or it's not work :-)

dvessel’s picture

Yur, that's really not the way to go. PHPTemplate provides a built-in system to handle .tpl.php files.

lucyconnuk, I named it wrong. I edited my original post. Try $vars['template_file'] not $vars['suggestion']. Excuse me. :)

lucyconnuk’s picture

Aha, finally got it working!

dvessel, I think the function name should be _phptemplate_variables not phptemplate_variables.

That makes the final working code:

function _phptemplate_variables($hook, $vars) {
  // Node is passed to the node when is rendered as a full page.
  if ($hook == 'page' && isset($vars['node'])) {
    $vars['template_file'] = 'page-'. $vars['node']->type;
  }
 
  return $vars;
}

.. This worked for me anyway.

Thanks for your help.

Lucy C

Mercury500’s picture

Anyway to get this to work in Version 6.2?

Or something like it? So I can use custom page.tpl.php files based on content type?

For instance, page-newstory.tpl.php.

Thanks.

m

vasheck’s picture

I do realize this reply is posted with a huge delay. However, I hope someone will find this useful.

Here's a solution for the same issue in D6.
Just place the following code in the head of the page.tpl.php file and create the new template with appropriate name (e.g. let's say I have a page created via my "general" content type).

      if ($node->type == 'general') {/* check if it's a general page */
        include 'page-general.tpl.php'; /*load a page-general.tpl.php */
        return; }   
   

http://drupal.org/node/45944

I hope this helps.

ledelboy’s picture

This really helps. Thanks a lot. Documentation is pretty scarce in this area.

dvessel’s picture

It is documented. http://drupal.org/node/223440

Do not manually include the files. It's not a good idea.

bombayhustle’s picture

This is much easier If you're using pathauto. You can do the following:

1. Set pathauto's Node Path settings for the story content type to story/[title-raw]
2. Rename page.tpl.php to page-story.tpl.php

And you're good to go.

The only caveat here is that all story content types will go under www.domain.com/story/... which doesn't seem like that bad of an idea to me.