Direct Output with Includes
When we want to utilize the convenience of direct output, but don't want to repeat the same markup in every template file, we move the code that we want to re-use into separate files. That way we can have multiple template files that pull in the same bits of code without us having to repeat ourselves. The benefit is that if we need to change something, we only need to change it in one place rather than in all of our template files.
To get started, lets figure out the parts of our basic-page.php template file that we want to re-use in other template files. In this case, we know that the markup at the top of all our template files will be the same, so lets move that to a head.inc file (or whatever you want to name it). We'll use the .inc extension for the file just to clarify that it is a file meant to be included in others, rather than a dedicated template file of it's own (though this naming convention is optional).
<html> <head> <title><?php echo
$page->title; ?></title> </head> <body> <h1><?php echo $page->title; ?></h1>
We also know that the markup at the end of all our template files will be the same, so we'll move that markup into a foot.inc file.
Where the template files will vary is what's in-between head.inc and foot.inc. So we'll use our basic-page.php template file to do the following, in this order:
- include the head.inc file
- output content
- include the foot.inc file
Here's how we might do that:
<?php include("./head.inc"); echo $page->body; include("./foot.inc");
The output is identical to when basic-page.php was an entire HTML document. But our template file has become a lot smaller, and we can re-use our head.inc and foot.inc in any other template files that we want to. For instance, we might have another template called sidebar-page (with file sidebar-page.php) that produces output similar to basic-page.php while being able to support separate bodycopy and sidebar columns:
<?php include("./head.inc"); ?> <div id='bodycopy'> <?php echo $page->body; ?> </div> <div id='sidebar'> <?php echo $page->sidebar; ?> </div> <?php include("./foot.inc"); ?>
Using includes for head.inc and foot.inc is just for starters. You can use includes for literally anything. Many people refer to these types of includes as partials. You can extend their use to anything that you might want to repeat or reuse in any template file.
Direct output with automatic inclusions
It's not actually necessary to manually
include("./foot.inc") in each of your template files. You can make this happen automatically by editing your /site/config.php file and populating these two lines like so:
$config->prependTemplateFile = 'head.inc'; $config->appendTemplateFile = 'foot.inc';
What the above essentially says is to automatically load the contents of head.inc file before your template file (prepend), and load the contents of foot.inc after your template file (append). The result is that it's no longer necessary to manually include those in each of your template files. These automatic inclusions actually come in handy in other template file strategies too, so keep them in mind!
Disadvantages of direct output
Where direct output starts to fall apart is when you want to affect the output in multiple regions on a page. In our examples above, our template files only control the output for what comes between head.inc and foot.inc. While we could accommodate more regions with additional includes, it starts to get a little cumbersome. What if any of our template files could populate any region in our markup without us having to know exactly where it will go ahead of time? That leads into our next strategy…
Next: Delayed Output »
Your e-mail is kept confidential and not included with your comment. Website is optional.