Community Documentation

Theme coding conventions

Last updated August 27, 2009. Created by jbrauer on June 14, 2003.
Edited by bekasu, jhodgdon, LeeHunter, peterx. Log in to edit this page.

Theme authors should take care to write clean, well structured code just like a coder for any other project. Doing so makes the code easier to read, understand and maintain. While different organizations have different conventions, it's usually best to follow the Drupal standards as this helps when collaborating or asking for help.

  • Add 2 spaces for indents; rather than a tabbed indent (This maintains consistency and is consistent with Drupal's coding standards)
  • Match the indentation of long opening and closing block HTML tags
  • Distinguish between PHP and HTML indentation.

    Not this:

      ...
      <?php if ($header): ?>
      <div id="header">
        <?php print $header; ?>
      </div>
      <?php endif; ?>
      ...
       

    but this:

      ...
    <?php if ($header): ?>
      <div id="header">
        <?php print $header; ?>
      </div>
    <?php endif; ?>
      ...
       

    This makes it much easier to find matching opening and closing tags defined with different indentation.

  • Prefer PHP in HTML to HTML in PHP in templates. For example,

    not this:

    <?php
    if (!$page) {
      print
    "<h2><a href=\"$node_url\">$title</a></h2>";
    }

    if (
    $submitted) {
      print
    "<span class=\"submitted\">$submitted</span>";
    }
    ?>

    but this:

    <?php if (!$page): ?>
      <h2><a href="<?php print $node_url; ?>"><?php print $title; ?></a></h2>
    <?php endif; ?>

    <?php if ($submitted): ?>
      <span class="submitted"><?php print $submitted; ?></span>
    <?php endif; ?>
       

    After all, PHP is a HTML-embedded scripting language, and not the other way around.

  • Separate logic from presentation.
    You sometimes see:
    Price: <?php print $price; ?>
    Tax: <?php print $price * 0.075; ?>
  • Move the calculation up the code before you start the presentation of the data. The business logic part of the script would contain:
    <?php $tax = $price * 0.075; ?>
    and the presentation part:

    Price: <?php print $price; ?>
    Tax: <?php print $tax; ?>
  • Always put a semicolon at the end of your small PHP printing statements:
    <?php print $tax; ?> -- YES
    <?php print $tax ?> -- NO
  • In more extended sections of PHP code, follow the Drupal coding standards.

Comments

Distinguish between PHP and HTML indentation

The text says to distinguish. The example says to NOT distinguish, i.e. indent PHP as if it were HTML and vice versa. For me it's like a contradiction.

Notice the dots

I believe the three dots represent the HTML indentation level before and after the example.
Notice how in the second example the PHP indentation is one indentation level shallower than the HTML indentation so the HTML is kept at the same depth as it was prior to the opening PHP tag? This assumes there wasn't an opening HTML tag just prior to the opening PHP tag, in which case the HTML would be one level deeper. That would make it two levels deeper than the PHP indentation at that point, making them even more distinguished.
I've tried to make a more complex example below to demonstrate this further.

HTML 1
  HTML 2
  HTML 2
    HTML 3
PHP 1
    HTML 3
  PHP 2
      HTML 4
  PHP 2
    PHP 3
      PHP 4
        PHP 5
      HTML 4
        PHP 5
      PHP 4    
    PHP 3
  PHP 2
PHP 1
  HTML 2
HTML 1

Anyone is of course welcome to correct me on this,