Templates can improve the structure of your site. This article explains how to use templates to control page layouts in a Web site composed of a large number of static HTML pages through a new feature and template class in PHP 4.
Outline:
===================================
Detach functionality and layout
Avoid page element duplication
Template framework for static web sites
===================================
Detach functionality and layout
First, let's take a look at the two main purposes of applying the template:
Detach function (PHP) and layout (HTML)
Avoid page element duplication
The first is to talk about the most, and it envisions a scenario where a group of programmers write PHP scripts to generate page content, while another group of designers design HTML and graphics to control the final appearance of the page. The basic idea of separating functions and layouts is to enable both groups to write and use separate sets of files: programmers only care about files that contain only PHP code, without caring about the appearance of the page, and page designers can design page layouts with their most familiar visual editors, No need to worry about breaking any PHP code embedded in the page.
If you've ever seen a few tutorials about PHP templates, you should already understand how the templates work. Consider a simple part of the page: the top of the page is the header, the left is the navigation bar, and the rest is the content area. This web site can have the following template files:
<!--main.htm-->
<body>
<table><tr><td>{HEADER}</td></tr>
<tr><td>{LEFTNAV}</td><td>{CONTENT}</td></tr>
</table>
</body>
<!--header.htm-->
<!--leftnav.htm-->
<br><a href= "foo" >Foo</a>
<br><a href= "Bar" >Bar</a>
You can see how pages are constructed from these templates: The main template controls the layout of the entire page; header templates and LeftNav templates control the common elements of the page. The identifier inside the curly brace "{}" is a content placeholder. The main advantage of using templates is that interface designers can edit them as they wish, such as setting fonts, modifying colors and graphics, or completely changing the layout of a page. Interface designers can edit these pages with any normal HTML editor or Visual tool, because they contain only HTML code and no PHP code.
The PHP code is saved in a separate file, which is the file that is actually called by the page URL. The Web server parses the file through the PHP engine and returns the results to the browser. Generally, PHP code always dynamically generates page content, such as querying a database or performing some sort of calculation. Here is an example:
<?php
example.php
Require (' class. Fasttemplate.php ');
$TPL = new Fasttemplate ('. ');
$TPL->define Array (' main ' => ' main.htm '),
' Header ' => ' header.htm ',
' LeftNav ' => ' leftnav.htm '));
The PHP code here is set $content to include the appropriate page content