|
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.
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.com | John Tyrrell | 03/08/05 14:12 |
| freaking BS, I enjoyed the IP | Freeflashonlinegames 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 IP | Freeflashonlinegames 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? | Zite | 01/29/04 09:50 |
| RE: FastClass Template Not For PHP 4.3.1 | Anas | 04/14/03 17:14 |
| FastClass Template Not For PHP 4.3.1 | Earthrise | 02/24/03 13:19 |
| HELP!!! I'm new.... | Dave | 12/13/02 19:02 |
| RE: The {} are not removed | Nico | 11/20/02 15:47 |
| FastTemplate | Chris | 10/22/02 14:48 |
| Linking with the template | Reaper | 08/22/02 11:13 |
| Chinese characters are corrupted. | Wendy Liu | 07/27/02 13:24 |
| Embedding PHP in templates | Nick | 07/17/02 00:29 |
| ZIP Please! | Dale | 06/29/02 06:37 |
| RE: is it possible...? | Nozmo | 05/21/02 15:27 |
| is it possible...? | Nozmo | 05/21/02 14:25 |
| RE: Using file.php?page=home | donoe | 04/12/02 13:36 |
| {$key} parsing | Ron Phillips | 03/18/02 11:34 |
| RE: php templates & parent directories | Eric C. Pollitt | 03/08/02 21:03 |
| RE: php templates & parent directories | Eric C. Pollitt | 03/05/02 16:41 |
| RE: My Solution | Anthony R. | 02/20/02 22:49 |
| RE: php templates & parent directories | rod | 02/13/02 21:01 |
| RE: Using file.php?page=home | Walter Horstman | 01/30/02 10:07 |
| frames check | aqua man | 01/02/02 17:34 |
| using template with pear | daniel | 01/02/02 01:35 |
| How to parse the "common elements" ? | Johan Svahn | 12/19/01 11:56 |
| Using file.php?page=home | Fleur Corfield | 12/19/01 11:34 |
| How to add a footer | John | 12/18/01 22:42 |
| RE: Passing variables from home to prepend | Samuel L. Diaz | 12/13/01 21:40 |
| RE: The {} are not removed | jerome bana | 10/03/01 19:31 |
| RE: how to add php script: HELP!!! | cgoodness | 08/28/01 07:01 |
| php templates & parent directories | Catie | 08/20/01 17:40 |
| template.php3 | Josef | 08/10/01 09:45 |
| does anyone still use this?? | jason matta | 07/23/01 23:52 |
| PHP and Frames | michael | 07/23/01 20:45 |
| Horrible Article! | Brandon Keim | 07/09/01 20:14 |
| Help on OB | Sherman Cahal | 06/13/01 21:24 |
| Modifications-4th Common Element | Earthrise | 05/18/01 18:03 |
| RE: Completely different way of using one fil | Alain Fontaine | 04/11/01 17:31 |
| Smarty template engine BETTER | syber | 04/06/01 17:56 |
| Smarty - the PHP compiling template engine | philip olson | 03/25/01 19:44 |
| RE: The {} are not removed | leslie | 03/14/01 22:53 |
| RE: Phplib's vs FastTemplate | basil | 03/11/01 21:29 |
| RE: Completely different way of using one fil | Chris Simmons | 02/16/01 02:31 |
| The {} are not removed | Bill Eddins | 02/14/01 10:18 |
| Completely different way of using one file | OkamiTsubasa | 02/11/01 17:49 |
| Admin. Tool and Content Publishing | Brian Flora | 01/19/01 20:34 |
| probs with cookies | wim | 01/13/01 08:28 |
| Slow?? | Peter | 01/04/01 10:17 |
| can it happen? | lin | 01/02/01 04:30 |
| RE: I'm confused as hell also | Chip | 01/02/01 01:50 |
| RE: I'm confused as hell also | jim | 12/30/00 02:32 |
| I'm confused as hell also | Chip | 12/25/00 01:18 |
| RE: class.FastTemplate.php | Matthew Kendall | 12/24/00 23:36 |
| RE: My Solution | Matthew Kendall | 12/24/00 23:23 |
| pixelcore | WeaponZero | 12/19/00 19:43 |
| My Solution | Jason | 12/15/00 16:44 |
| Confused as hell | iucpxleps | 12/10/00 07:56 |
| Embedding php in templates | jim | 12/10/00 07:29 |
| RE: how to add php script | jim | 12/09/00 03:46 |
| Use php to create templates for php | Daniel Staver | 12/07/00 12:49 |
| RE: Problems with warning | Andrey | 12/04/00 15:23 |
| how to add php script | Karl | 12/04/00 13:16 |
| Problems with warning | Frederic | 12/04/00 10:32 |
| Passing variables from home to prepend | Allen McLeod | 12/04/00 03:20 |
| RE: Phplib's vs FastTemplate | Andrey | 12/02/00 14:26 |
| New Window.... | James | 11/30/00 16:09 |
| RE: The big consideration | Scott Molinari | 11/28/00 15:52 |
| class.FastTemplate.php | Marceu Filho | 11/27/00 12:32 |
| The big consideration | Cliff Baeseman | 11/27/00 12:20 |
| RE: the zip archive | Kelly Jones | 11/26/00 15:02 |
| RE: Phplib's vs FastTemplate | Tom Anderson | 11/26/00 03:46 |
| the zip archive | Chip | 11/25/00 15:41 |
| RE: Easier ways to get the same effect | ashley etchell | 11/25/00 14:05 |
| Other Template Systems | Brian Snipes | 11/24/00 22:10 |
| take a look!! | Thanasis | 11/24/00 08:55 |
| RE: Phplib's vs FastTemplate | Damien Bonvillain | 11/24/00 05:33 |
| RE: Phplib's vs FastTemplate | Peter | 11/23/00 05:32 |
| RE: The template approach | jan janssen | 11/23/00 04:33 |
| RE: Is it only for "read-only" things ? | Paul K Egell-Johnsen | 11/23/00 04:01 |
| RE: The template approach | Paul K Egell-Johnsen | 11/23/00 04:00 |
| RE: The template approach | Liron Levy | 11/23/00 02:41 |
| RE: Phplib's vs FastTemplate | Benjamin Smith | 11/23/00 02:13 |
| Is it only for "read-only" things ? | Habibi | 11/23/00 00:53 |
| RE: The template approach | Jim Hawley | 11/22/00 15:44 |
| RE: The template approach | Anderson Fortaleza | 11/22/00 14:10 |
| RE: Phplib's vs FastTemplate | Jan Lehnardt | 11/22/00 12:45 |
| RE: The template approach | Paul K Egell-Johnsen | 11/22/00 11:19 |
| The template approach | Liron Levy | 11/22/00 10:50 |
| Phplib's vs FastTemplate | Alex Verstraeten | 11/22/00 10:41 |
| Easier ways to get the same effect | Auke van Slooten | 11/22/00 06:41 |
| auto-prepend/append | Andy Dickinson | 11/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. | ||


