UNIX Systems Administrator (IL)
Next Step Systems
US-IL-Chicago

Justtechjobs.com Post A Job | Post A Resume

A Template Framework for Static Sites
A Template Framework for Static Sites
First, we write template files like the ones above for all the common elements of our pages, and the overall page layout. We remove all the common elements from our page files, leaving just the page content. Then we add three lines of PHP to each page, like this:

<?php

<!-- home.php -->
<?
php require('prepend.php'); ?>
<?php pageStart
('Home'); ?>

<h1>Hello World</h1>
<p>Welcome to my web site.</p>
<img src="demo.jpg" alt="demo image">
<p>I hope you like it.</p>

<?php pageFinish(); ?>

?>
This has largely addressed the problems. There are just three lines of PHP in the file, none of which contain any template-related code and are thus never likely to need changing. And the HTML is outside the PHP tags and thus does not need special characters escaping. We can easily add these three lines of PHP to all our static HTML pages.
The require function includes a PHP file which contains all the necessary template-related PHP code. The pageStart function sets up the template object (and sets a page title), and the pageFinish function parses the template and generates the output for the browser.
How is this done? Why does the HTML in this file not get sent to the browser before the pageFinish function gets called? The answer is a new feature introduced in PHP4 that allows output destined for the browser to be captured in a buffer. A look at the prepend.php file shows how this works:

<?php

require('class.FastTemplate.php');

function
pageStart($title = '') {
  GLOBAL
$tpl;
  
$tpl = new FastTemplate('.');
  
$tpl->define( array( 'main' => 'main.htm',
                       
'header' => 'header.htm',
                       
'leftnav' => 'leftnav.htm' ) );
  
$tpl->assign('TITLE', $title);
  
ob_start();
}

function
pageFinish() {
  GLOBAL
$tpl;
  
$content = ob_get_contents();
  
ob_end_clean();
  
$tpl->assign('CONTENT', $content);
  
$tpl->parse('HEADER', 'header');
  
$tpl->parse('LEFTNAV', 'leftnav');
  
$tpl->parse('MAIN', 'main');
  
$tpl->FastPrint('MAIN');
}

?>
The pageStart function instantiates a template and sets it up, then turns on output buffering. Now, all HTML from the page itself is captured in the buffer. The pageFinish function extracts the buffered content and uses it to define the template object's content before parsing the templates and outputting the finished page.
pageStart flow chart
That's it. Write template files containing HTML fragments to define your site's common elements. Delete all the common page layout stuff from all your pages and replace with three lines of PHP you will never need to change. Put the FastTemplate class file and the prepend PHP file in your include path. Now you have a web site where the page layout is centrally controlled, enhancing reliability and maintainability and making site-wide changes easy.
Resources
Zipped archive of the files in this article, including a working example site and with more comments in the code than shown here.
The FastTemplate class can be found at http://www.thewebmasters.net/. The latest version is 1.1.0 and there is a small patch to apply to ensure correct operation with PHP4. Alternatively, the zipped archive above includes the class already patched.
Acknowledgments
The functions in prepend.php are based on ones (without output buffering) described in "Professional PHP Programming" by Harish Rawat, et al.
-- Matthew

[Page 1]  [Page 2]  [Page 3]  [Page 4]  [Page 5]  


Comments:
Агентство недвижимости БАЗИСАгентство недвижимости БАЗИС04/30/07 00:06
john@hotmail.comJohn Tyrrell03/08/05 14:12
freaking BS, I enjoyed the IPFreeflashonlinegames Com 12/03/04 18:48
it tells me i don't have Freeflashonlinegames Com 12/03/04 18:41
Thanks. I hope other people test it too. Sex Beplaced Ru 12/03/04 18:19
If not, what were all the witch trials about? Sex Beplaced Ru 12/03/04 18:12
PLease, how Can i download. Sex Beplaced Ru 12/03/04 18:12
freaking BS, I enjoyed the IPFreeflashonlinegames Com 12/03/04 18:12
This was closely associated with the rebirth of Sex Beplaced Ru 12/03/04 18:09
ummmmmmmmm whats this mummy??Freeflashonlinegames Com 12/03/04 18:09
it tells me i don't have Sex Beplaced Ru 12/03/04 17:57
This was closely associated with the rebirth of Freeflashonlinegames Com 12/03/04 17:53
Need help?Zite01/29/04 09:50
RE: FastClass Template Not For PHP 4.3.1Anas04/14/03 17:14
FastClass Template Not For PHP 4.3.1Earthrise02/24/03 13:19
HELP!!! I'm new....Dave12/13/02 19:02
RE: The {} are not removedNico11/20/02 15:47
FastTemplateChris10/22/02 14:48
Linking with the templateReaper08/22/02 11:13
Chinese characters are corrupted.Wendy Liu07/27/02 13:24
Embedding PHP in templatesNick07/17/02 00:29
ZIP Please!Dale06/29/02 06:37
RE: is it possible...?Nozmo05/21/02 15:27
is it possible...?Nozmo05/21/02 14:25
RE: Using file.php?page=homedonoe04/12/02 13:36
{$key} parsingRon Phillips03/18/02 11:34
RE: php templates & parent directoriesEric C. Pollitt03/08/02 21:03
RE: php templates & parent directoriesEric C. Pollitt03/05/02 16:41
RE: My SolutionAnthony R.02/20/02 22:49
RE: php templates & parent directoriesrod02/13/02 21:01
RE: Using file.php?page=homeWalter Horstman01/30/02 10:07
frames checkaqua man01/02/02 17:34
using template with peardaniel01/02/02 01:35
How to parse the "common elements" ?Johan Svahn12/19/01 11:56
Using file.php?page=homeFleur Corfield12/19/01 11:34
How to add a footerJohn12/18/01 22:42
RE: Passing variables from home to prependSamuel L. Diaz12/13/01 21:40
RE: The {} are not removedjerome bana10/03/01 19:31
RE: how to add php script: HELP!!!cgoodness08/28/01 07:01
php templates & parent directoriesCatie08/20/01 17:40
template.php3Josef08/10/01 09:45
does anyone still use this??jason matta07/23/01 23:52
PHP and Framesmichael07/23/01 20:45
Horrible Article!Brandon Keim07/09/01 20:14
Help on OBSherman Cahal06/13/01 21:24
Modifications-4th Common ElementEarthrise05/18/01 18:03
RE: Completely different way of using one filAlain Fontaine04/11/01 17:31
Smarty template engine BETTERsyber04/06/01 17:56
Smarty - the PHP compiling template enginephilip olson03/25/01 19:44
RE: The {} are not removedleslie03/14/01 22:53
RE: Phplib's vs FastTemplatebasil03/11/01 21:29
RE: Completely different way of using one filChris Simmons02/16/01 02:31
The {} are not removedBill Eddins02/14/01 10:18
Completely different way of using one fileOkamiTsubasa02/11/01 17:49
Admin. Tool and Content PublishingBrian Flora01/19/01 20:34
probs with cookieswim01/13/01 08:28
Slow??Peter01/04/01 10:17
can it happen?lin01/02/01 04:30
RE: I'm confused as hell alsoChip01/02/01 01:50
RE: I'm confused as hell alsojim12/30/00 02:32
I'm confused as hell alsoChip12/25/00 01:18
RE: class.FastTemplate.phpMatthew Kendall12/24/00 23:36
RE: My SolutionMatthew Kendall12/24/00 23:23
pixelcoreWeaponZero12/19/00 19:43
My SolutionJason12/15/00 16:44
Confused as helliucpxleps12/10/00 07:56
Embedding php in templatesjim12/10/00 07:29
RE: how to add php scriptjim12/09/00 03:46
Use php to create templates for phpDaniel Staver12/07/00 12:49
RE: Problems with warningAndrey12/04/00 15:23
how to add php scriptKarl12/04/00 13:16
Problems with warningFrederic12/04/00 10:32
Passing variables from home to prependAllen McLeod12/04/00 03:20
RE: Phplib's vs FastTemplateAndrey12/02/00 14:26
New Window....James11/30/00 16:09
RE: The big considerationScott Molinari11/28/00 15:52
class.FastTemplate.phpMarceu Filho11/27/00 12:32
The big considerationCliff Baeseman11/27/00 12:20
RE: the zip archiveKelly Jones11/26/00 15:02
RE: Phplib's vs FastTemplateTom Anderson11/26/00 03:46
the zip archiveChip11/25/00 15:41
RE: Easier ways to get the same effectashley etchell11/25/00 14:05
Other Template SystemsBrian Snipes11/24/00 22:10
take a look!!Thanasis11/24/00 08:55
RE: Phplib's vs FastTemplateDamien Bonvillain11/24/00 05:33
RE: Phplib's vs FastTemplatePeter11/23/00 05:32
RE: The template approachjan janssen11/23/00 04:33
RE: Is it only for "read-only" things ?Paul K Egell-Johnsen11/23/00 04:01
RE: The template approachPaul K Egell-Johnsen11/23/00 04:00
RE: The template approachLiron Levy11/23/00 02:41
RE: Phplib's vs FastTemplateBenjamin Smith11/23/00 02:13
Is it only for "read-only" things ?Habibi11/23/00 00:53
RE: The template approachJim Hawley11/22/00 15:44
RE: The template approachAnderson Fortaleza11/22/00 14:10
RE: Phplib's vs FastTemplateJan Lehnardt11/22/00 12:45
RE: The template approachPaul K Egell-Johnsen11/22/00 11:19
The template approachLiron Levy11/22/00 10:50
Phplib's vs FastTemplateAlex Verstraeten11/22/00 10:41
Easier ways to get the same effectAuke van Slooten11/22/00 06:41
auto-prepend/appendAndy Dickinson11/22/00 02:22
 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.

Add A Comment:

Name:

Email:

Subject:

Message:

To reduce spam posts, messages are now manually approved

You are not [logged in]. That means your account will not get credit for this post.