1

This is very similar to this, hopefully its different enough to warrant its own question: How to display a field before node title?.

On our Drupal sites we have a dozen content types, many of which have custom node templates to control how they are rendered. We like this setup because our design/web production team can edit the template files easily but not more heavy drupal code.

However, we currently have only 1 page.tpl.php (we have a custom theme loosely based on bartik). In this template, we have ugly if-statements to make the page print different things based on the content type. Unfortunately, as the number of nodes increases this page.tpl.php is becoming unwieldy. The problem is that each node usually has different fields printed above and below the title, and usually modifies the ID of the title attribute.

What are the alternatives to adding more IF-statements into the page.tpl.php?

  • Move the printing of the title (and content above and below the title) down into each node.tpl.php
  • Modify the way that we are adding content above and below the title to use the existing title_prefix and title_suffix arrays
  • other?

If we switch to render arrays method, I worry that coming up with the ordering and formatting of the content above and below the title will become more difficult for non-coders.

4 Answers 4

1

It would be a much better practice to move most of this logic to the theme's preprocess_page: http://api.drupal.org/api/drupal/includes--theme.inc/function/template_preprocess_page function so you can keep the separation of logic and presentation and make things easier on your designers.

You could add variables that could contain the different fields and classes that change based on the nodes content type in the preprocess_page.

2
  • the logic has been separated from the presentation. the things that we are doing in page.tpl.php is saying if event (print event), etc. There are just many combinations of outputs for each different node type. Its really only the "title area" that has this issue, since there are many variations of displaying the title and subtitle line.
    – Joel
    Aug 3, 2011 at 13:25
  • If your logic (PHP if statements) is in your presentation (page.tpl.php) then they aren't separate ;) . You could reduce that to one or two variables that are managed in the template.php file using template_preprocess_page() . That seems like it would satisfy your goal of making the page.tpl.php less unwieldy. Is there something else you are trying to satisfy?
    – acouch
    Aug 3, 2011 at 14:11
1

I would rather implement hook_preprocess_page() to add suggestions for the template files used instead of page.tpl.php.

function mymodule_preprocess_page(&$variables) {
  if (!empty($variables['node'])) {
    if (empty($variables['theme_hook_suggestions'])) {
      $variables['theme_hook_suggestions'] = array()
    }

    array_unshift($variables['theme_hook_suggestions'], 'page__' . str_replace(array("/", "\\", "\0", '-'), array('', '', '', '_'), $variables['node']->type));
  }
}

In this way, for the "Blog" content type, the template file used would be page--blog.tpl.php, if that file exists. In that way, there would not be the need to use IF-statements that change the output basing on the content type of the node being shown.

If the part that would change from a template file to the other is minimal, then (as suggested by acouch) I would implement template_preprocess_page() in the template.php file. The logic part should be kept in that file, rather than in the template files.

1
  • Thank you for the quick response. If I could ask a follow up... we debated using the above method. It seemed like there would be a pretty big downside which is maintenance. In our case we have about 100 sites running on this code, and maintenance is a concern. Since page.tpl.php is so large, we don't want a lot of copies of it floating around because the chances of us having to tweak it in the future are fairly high. We would have to have a nearly identical copy of page.tpl.php every time we want to format the page title, subtitle, etc, right? Is there a way to override just one part?
    – Joel
    Aug 2, 2011 at 23:41
0

Personally, I don't like the multiple page.tpl.php solutions. I prefer to have one of these that gets used for everything, that also gets kept simple.

We typically employ a solution based on Panels. In your case, you can create a Node View template along with content type selection rules. The possibilities are pretty endless once you get to this point. You really need to try it out to see what is possible, but you can do what you are asking without much work.

0

Use Display suite and display suite extra. Goto admin/structure/ds/list/extras and enable "Page title options" in the "Other" tab. then you can disable default page title and place yours.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.