开发者

Drupal 6 Page templates depending on node type

I am trying to theme node page based on content type. Here I tried to follow the instructions as in http://drupal.org/node/249726.

What I did , copied node.tpl.php file to my theme directory. Renamed it as page-node-mycontenttype.tpl.php and wrote preprocess function in my template file as shown in above link.

Apparently found that It displays only the Node contents but not the common layout elements(HTML) (logo,header, footer and sidebars etc. which is defi开发者_如何学编程ned in page.tpl.php).

So is it necessary for me to define same common layout elements(HTML) once again (those defines in page.tpl.php) in page-node-mycontenttype.tpl.php?

If so then I have to manage 2 template files. Any HTML changes need to be done twice in both template files.

Is there any better way of having common layout template file referred by both page and node content type only the middle content area is coming from two different files (page or node content type)?

Could you please suggest common practice and way to achieve this?

Note: Make sure that to override node template file with respect to content type then both template files node.tpl.php and node-[content_type].tpl.php file need to be in your theme directory.


The file you're looking for is node-mycontenttype.tpl.php . You want a node template, not a page template. If you rename page-node-mycontenttype.tpl.php to node-mycontenttype.tpl.php, you'll get the result you're expecting.

The page template (page.tpl.php) in Drupal defines the overall markup and where the contents of the various regions will be outputted. Traditionally it includes the logo, menu, footer message, and a few other static elements. The Drupal theme system allows you to override the entire page template for a specific node type (which is what you ended up doing), but based on your explanation, what you're really trying to do is override just the node template.

Further reading: Explanation & diagram of what files control which pieces of output: http://drupal.org/node/171194 General documentation on overriding output in the theme system: http://drupal.org/node/341628

When reading the above, make sure to note the version of Drupal that it applies to (there's a tag up at the top of each page). Things changed fairly dramatically between 5 and 6 and have shifted again (though not nearly as much) between 6 and 7.


t-dub is right about node-contenttypename.tpl.php.

However, if you want to retheme the entire page for a given content type (maybe a landing page), you have two options (there are likely other options but these are the ones I've discovered.

  1. You may use a module called sections and then create a theme for each of the different content types.

OR

  1. You can add the following function to your theme's template.php:
function templatename_preprocess_page(&$vars) {
  // Template change based on node->type
  if (isset($vars['node'])) {
    // Add template naming suggestion. It should alway use hyphens.
    $vars['template_files'][] = 'page-'. str_replace('_', '-', $vars['node']->type);
  }
}

Just swap out templatename (in the function name) for the name of your template. Then you can create page-contenttypename.tpl.php for a unique design for nodes of that content type.

I've used sections in the past, but I prefer using the method described in #2 because it feels more drupal-ly. Its also a lot simpler to maintain.

Hope this helps someone!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜