How to overide page.tpl.php for specific CCK type

awasson - June 22, 2007 - 19:12

Hi there,
I am modifying a site I launched a few months ago so that it can have a new cck content type.

The new pages are going to be fairly plain compared to the rest of the site so I figured I would make a new template like page-plain.tpl.php and switch to it if the content type matched my new cck type.

Make sense so far?

Now what I'm trying to figure out is what I can use as a hook to detect my new content type. The machine readable name is "video_clip".

I'm using the following to switch templates for my home page:
if ($is_front) {
include('page-front.tpl.php');
return;
}

Is there something similar I can use to detect my CCK content types?

Thanks,
Andrew

I'm fairely new with CCK

TPerkins - June 22, 2007 - 19:45

I'm fairely new with CCK also, (just downloaded the module this morning) so I'm probably completely off. I know you can do that exact thing with CCK node types so I would assume you could do the same for pages.

For example if you have a CCK Node type called "article" and wanted to have a unique template just for it. You would create a file in with all your other templates called "node-article.tpl.php". That template would then be used autimatically for all nodes with the CCK type of 'article'.

So if your CCK page type were called 'video_clip', you could probably just make a template called, 'page-video_clip.tpl.php' and it would autimatically work.

Thanks it works with a little help

awasson - June 22, 2007 - 20:55

Thanks TPerkins,
With a little help from PhilK (below) it all came together.

I made a template called: page-video_clip.tpl.php

It won't work all by itself. You have to add a little code to the top of your page.tpl.php page:
if ($node->type == 'video_clip') {
include ('page-video_clip.tpl.php');
return;
}

Cheers,
Andrew

node->type

Phillip Mc - June 22, 2007 - 20:07

You can use the $node->type variable to switch page layouts in the same way as you did with the $is_front variable.

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

Recommend you check out Dublin Drupallers phptemplate snippets section of the handbook:

Drupal Handbooks » Customization and theming » PHPTemplate Theme Snippets » Customising full page layouts and sections » Customising the full page layout and sections based on node type

There's a bundle of brilliant snippets in there. Well worth bookmarking for future reference.

Phil.

Thanks PhilK, That did it!

awasson - June 22, 2007 - 20:57

Thanks PhilK,
That did it! Works like a charm.

Great link to full page editing by the way. I've got it bookmarked now. I don't know how I overlooked that.

Cheers,
Andrew

More portable solution.

j0rd - January 28, 2008 - 14:15

Put this code at the top of your page.tpl.php. Read the comment in the code to see what it does.

<?php
/* This block of code checks to see if you've created a page template for your particular node type.
   If so, it uses it! If not, it defaults to page.tpl.php
   */
.
$node_type_page_template = getcwd() . "/" . $directory . "/page-" . $node->type . ".tpl.php";
if(
file_exists($node_type_page_template)) {
   include(
$node_type_page_template);
   return;
}
?>

I've also similar setup and

DaveS98 - January 28, 2008 - 15:06

I've also similar setup and I use following names for files which automatically gets accessed without including anything in page.tpl.php

E.g. page-node-123.tpl.php

If I remove this file then by default page-tpl.php is used.

Isn't also a correct way?

Cheers
Dave

It should be...

deleuje - September 10, 2008 - 07:10

It should be according to all tutorials and screencasts I have seen on templating. But that simple rule didn't work with me while trying to theme OG pages on my setup. (Guess you are a lucky one.)

The above code, fortunately solved this problem for now...

Thank you, j0rd!

chrisdoherty - June 29, 2008 - 23:39

I was unable to get Drupal to recognize some pages I created with CCK until I tried this solution. This works great with Drupal 6 and it's so concise!

 
 

Drupal is a registered trademark of Dries Buytaert.