Theme coding conventions

Last modified: August 27, 2009 - 02:47

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.

Embedding PHP/HTML

seancr - July 22, 2009 - 00:30

Saying that PHP is intrinsically embedded in HTML is at the very least oversimplifying things.

I think this is a discussion

syn4k - July 25, 2009 - 00:58

I think this is a discussion of practice and style...but I would like to add that it does not, "in my humble opinion", make anything "clearer" to call as many php tags as you have done above. Further, in my opinion, php can/should have as few <?php statements as possible...especially if your document is indicating .php in the title. It is redundant and uneeded to have more than one of these statements in your code and can also cause people to get very confused very fast...especially if your document has many complex nested logical statements.

<?php
if ($something)
{
   print
$something;
}
?>

always makes more sense than

<?php if ($something): ?>
   <?php print $something; ?>
<?php endif; ?>

Yes, it can be understood...but painfully. And most of the lower level programmers I have worked with would agree.
Lastly, I feel that code should not only be clear but it should also be elegant in style and approach. Code should be self documenting. One of the good/bad things about PHP is that you can write it in many ways. Why not write it in ways that make sense to the majority of developers?

Shall I also say something about separating your logic from your presentation?

 
 

Drupal is a registered trademark of Dries Buytaert.