Rethinking the Structure of My Templates

Over the past few years I've been working on an initiative which shares a single template over several websites. The overall process has gone fairly well, but my standard process for building templates in PHP isn't holding so well. The number of files could use some pruning and there have been issues with variables and helper functions not being available when needed. Plus, it would be nice to have a common place where overall website setting could be changed to affect all pages. So it's time to rethink my template-building process.

Old Templates

Before getting into the new template setup, let's look at what they used to look like. Each component was stored in its own file and imported with the PHP require() function.

<html>
<head>
<title>Page Title</title>
<?php require "{$_SERVER['DOCUMENT_ROOT']}/inc/headContent.html"; ?>
</head>
<body>
<?php require "{$_SERVER['DOCUMENT_ROOT']}/inc/navigation.html"; ?>
<p>Main page content goes here.</p>
<?php require "{$_SERVER['DOCUMENT_ROOT']}/inc/footer.html"; ?>
</body>
</html>

This setup has worked well for the dozen or so years I've developed websites. However, some of those websites are increasing in complexity and involve more database-driven content that's shared among several websites. There has also been a growing need to perform common operations to the entire website prior to the open <html> tag. As you can see, there isn't a spot for these operations without adding another require() statement.

New Template Setup

Instead of splitting the template into several files, they're merged into one PHP class. The class is imported and utilized right away in the script.

<?php
require "{$_SERVER['DOCUMENT_ROOT']}/inc/class_mainTemplate.php";
$template = new mainTemplate();
?>

<html>
<head>

The class constructor method can now be used to lay any necessary ground work before executing the rest of the script. If many scripts depend on the date/time, for example, we may need to modify the default time zone.

<?php
class mainTemplate {
     //CONSTRUCTOR FUNCTION
     public function __construct() {
          //SET THE TIMEZONE
          date_default_timezone_set('America/Chicago');
     }
}
?>

Next, we'll build methods for each component of the template. These will replace the individual require() calls in the old template.

<?php
class mainTemplate {
     //CONSTRUCTOR FUNCTION
     public function __construct() {
          //SET THE TIMEZONE
          date_default_timezone_set('America/Chicago');
     }
 
     //FUNCTION TO DISPLAY THE <head> TAG CONTENT
     public function headContent() {
          //...head tag content here
     }
 
     //FUNCTION TO DISPLAY THE PAGE HEADER AND MAIN NAVIGATION
     public function navigation() {
          //...page header and navigation information here
     }
 
     //FUNCTION TO DISPLAY THE PAGE FOOTER
     public function footer() {
          //...footer information here
     }

}
?>

Then we just replace the require() calls with the corresponding class method calls.

<?php
require "{$_SERVER['DOCUMENT_ROOT']}/inc/class_mainTemplate.php";
$template = new mainTemplate();
?>
<html>
<head>
<?php $template->headContent(); ?>
</head>
<body>
<?php $template->navigation(); ?>
<p>Main page content goes here.</p>
<?php $template->footer(); ?>
</body>
</html>

Conclusion

In addition to having a place to modify overall website settings, like the time zone, the new template has other benefits. The overall template is now stored in the same file. No more going back and forth between files to make sure the proper HTML tags opened in one are closed in the other. Tracking the usage of variables throughout the template is also easier. I know where they're initialized and used. Since the variables are defined within a class, there's little to no chance of conflicting variables due to variable scope.

If you have any suggestions to improve my template setup or would like to share your process, feel free to use the comments section below.

0 Comments

There are currently no comments.

Leave a Comment


Warning: Undefined variable $user_ID in /home/cyberscorp/webdev.cyberscorpion.com/wp-content/themes/scorpbytes/comments.php on line 72