2

I'm primarily a designer and I've been working on Drupal themes for some time now. At this point, I'm comfortable enough converting my designs to Drupal themes reasonably quickly but I continue to have doubts about how to use the template.php file... I've read up about it quite a bit, but my understanding of its usefulness is theoretical at best.

Clearly it's not a required file since it's never stopped my themes from working in the past, but is it advisable to have one in all cases? Are there functions (or anything else for that matter) one would automatically include in template.php?

2

The template.php file should contain the presentation code; the "logic" code, such as the theme functions, or other code that computes the value of a variable, should be in the template.php file.

To make an example, code similar to the following one should not be in the template file, but it should be put in a function contained in the template.php file, and which return a value to the calling code, which then uses the returned code:

  if ($user->uid) {
    $class[] = 'authenticaded-user';

    if (user_access('administer content')) {
      $class[] =  'content-administrator';
    }
  }
  else {
    $class[] = 'anonymous-user';
    $class[] = 'anonymous-' . strtr(ip_address(), '.', '-'); 
  }

  if (isset($class)) {
    print ' class="' . implode(' ', $class) . '"';
  }

The code in the template file should just print (or render) values, but not compute those values; clearly, it's perfectly fine to use code similar to the following one (the code is taken from a Drupal 7 theme):

    <?php if ($logo || $site_title): ?>
      <?php if ($title): ?>
        <div id="branding"><strong><a href="<?php print $front_page ?>">
        <?php if ($logo): ?>
          <img src="<?php print $logo ?>" alt="<?php print $site_name_and_slogan ?>" title="<?php print $site_name_and_slogan ?>" id="logo" />
        <?php endif; ?>
        <?php print $site_html ?>
        </a></strong></div>
      <?php else: /* Use h1 when the content title is empty */ ?>
        <h1 id="branding"><a href="<?php print $front_page ?>">
        <?php if ($logo): ?>
          <img src="<?php print $logo ?>" alt="<?php print $site_name_and_slogan ?>" title="<?php print $site_name_and_slogan ?>" id="logo" />
        <?php endif; ?>
        <?php print $site_html ?>
        </a></h1>
    <?php endif; ?>
    <?php endif; ?>

The difference is that, in this case, the code is verifying if the variable contains a useful value, and eventually avoid to output the markup, when it's not necessary.

3
  • OK thanks, but would you say it's strongly advisable to have a template.php file or is it optional? – Jane Aug 27 '11 at 16:48
  • If the code in your template file is similar to the second example I made, then there is no need to use a template.php; if the code is similar to the first example, then I would strongly suggest to use the template.php file. – kiamlaluno Aug 27 '11 at 16:57
  • I've always been kind of peeved about the "logic goes in template.php" guideline. I agree with it, but when the .tpl.php is then littered with if(){}else{}s and other logic it's kind of hard to take the guideline seriously. Don't mind me, just being a cranky old man... – Entendu Aug 30 '11 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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