Internet Bandaid   [RSS Feed]

Less PHP code in template – Best Practice

without comments

I was reviewing code written by a new team member and wrote a suggestion on how to improve. He wrote a view file that looked like this:

// listView.php
<?php

class vehicleListView
{

  function render($list)
  {
    echo "<table border='1'>
    <tr>
    <th></th>
    <th>ID</th>
    <th>Model</th>
    <th>Weight(kg)</th>
    <th>Year</th>
    </tr>";
    foreach($list->getData() as $vehicle)
    {
      $this->vehicleRender($vehicle->getData());
    }
    echo "</table>";
  }

  function vehicleRender($vdata)
  {
    echo "<tr>";
    echo "<td>". "<a href=\"/automobile-edit.php?id=" . $vdata['automobile_id']."\">edit</a>" ."</td>";
    echo "<td>" . $vdata['automobile_id'] . "</td>";
    echo "<td>" . $vdata['car_model'] . "</td>";
    echo "<td>" . $vdata['weight'] . "</td>";
    echo "<td>" . $vdata['manufacture_year'] . "</td>";
    echo "</tr>";
  }
}

?>

This was my response to him:

I see that your listView.php (which is a view) is structured as a class with functions that echo ‘<html>content</html>’. This should change because we often work with other specialized front end coders (or journalists who do data entry into the front end code) who do not know PHP and are scared by too much echo statements, php functions, class declarations etc…. Specialized front end coders only know html, css and “maybe” javascript.

So what you should do is set up your view in such a way that it hardly has any php code. In fact, try to keep your php code to only

  • if statements that are only 1 level deep as opposed many nested if statements
  • for loops (but try not to nest too many for loops in if statements and vice versa)
  • echo statements that only echo a maximum of 1 line as opposed to entire chunks of html
  • include statements to get other templates
  • simple content formatting functions like number_format(), substr(), and maybe even count()

Here is one suggestion, but there are plenty of other ways to do it as well. For example:

// templates/header.php
<html>
<body>

// templates/footer.php
</body>
</html>

// templates/list.php
<table>
<?php foreach($dataset as $row) : ?>
<tr>
<td><?php echo $row['column1']; ?></td>
</tr>
<?php endforeach; ?>
</table>

//listView.php
function someobjectfunction()
{
$content = '';
$dataset = getsomethingfrommodel();
ob_start();
include('templates/header.php');
include('templates/list.php');
include('templates/footer.php');
$content = ob_get_clean();
return $content;
}

This is one of the “popular” ways our team has been doing it lately. But i’m sure there are plenty of other ways to do it if you want to explore.

If you study other open source platforms like WordPress, Magento, Drupal, Code Igniter, etc…, you will notice that their view files are often held in a separate folder with MINIMUM php code … basically it tries its best to show straight html code. Sometimes the view files will have it’s own “templating” language like smarty, or aspx, etc…

So in the end, try to make your view files as friendly as possible for people who know only html and css.

Written by John Lai

March 15th, 2014 at 8:56 am

Posted in Uncategorized

Start Mission - Project management and invoicing

Leave a Reply