The PHP code goes in a separate file. This is the file that is actually called
up by the page URL. The web server uses the PHP engine to parse this file and
returns the result to the browser. Typically the PHP code produces the content
for the page dynamically, perhaps by querying a database or performing a
calculation. The file might be as follows:
up by the page URL. The web server uses the PHP engine to parse this file and
returns the result to the browser. Typically the PHP code produces the content
for the page dynamically, perhaps by querying a database or performing a
calculation. The file might be as follows:
<?php
// example.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
// PHP code here to set $content to the required page content
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
We are using the popular FastTemplate class here, but the idea is the same for
many of the available template classes. You instantiate the class, tell it
where to find the template files, and tell it which template file corresponds
to which page element. You generate the content for the page, perhaps by
querying a database, and assign the result to an identifier. Then you parse
the template files one by one and the template class performs the required
substitutions for you. Finally you output the resulting string to the browser.
many of the available template classes. You instantiate the class, tell it
where to find the template files, and tell it which template file corresponds
to which page element. You generate the content for the page, perhaps by
querying a database, and assign the result to an identifier. Then you parse
the template files one by one and the template class performs the required
substitutions for you. Finally you output the resulting string to the browser.
The key benefit is that this file is entirely PHP. There is no HTML in it. The
PHP programmer can now concentrate on writing the code to generate the page
content without having to worry about generating HTML to format it nicely for
the final page.
PHP programmer can now concentrate on writing the code to generate the page
content without having to worry about generating HTML to format it nicely for
the final page.
You can build a whole site using this technique and just these files. If the
code that generates the page content does so on the basis of a query string in
the URL, you could build a magazine, for example, using a URL like
http://www.foo.com/example.php?article=099
code that generates the page content does so on the basis of a query string in
the URL, you could build a magazine, for example, using a URL like
http://www.foo.com/example.php?article=099
On the face of it, it looks like we have a second benefit too. In the example,
the left-hand navigation bar is in its own file. We can change how it looks on
every page on our site by editing this one template file.
the left-hand navigation bar is in its own file. We can change how it looks on
every page on our site by editing this one template file.