| php The What and Why of Templates |
In PHP, the most common method of separating presentation and application logic is through the use of templates. A template is (in general) an HTML document that contains special markers and/or control structures. In fact, PHP was originally designed to be a simple macro language that functioned much like a template engine.
Separating Common Elements from Code
As PHP grew more popular, it was quickly adopted by Web developers across the world because it was very easy to learn. This ease of development made PHP one of the best languages for rapid application development and prototyping. Unfortunately, the same capabilities that make PHP such an excellent language for prototyping quickly also make it very easy to create unmanageable code. Developers soon realize that as websites become larger, making their websites more modular becomes very important. The most common solution to this problem is to separate the website into common elements that can be included via a PHP include statement. For instance, in most cases, you can separate any given website into three separate elements: a header, a footer, and the actual content. shows how to separate your average Web page into three segments:
Listing 7.1. Your Typical Segmented Web Page
segments.php
<?php
function display_head($title="Your typical web page") {
?>
<HTML>
<HEAD><TITLE><?=$title?></TITLE></HEAD>
<BODY>
<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR>
<TD>
<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD><A href="products.php">Products</A></TD></TR>
<TR><TD><A href="contact.php">Contact</A></TD></TR>
<TR><TD><A href="about.php">About Us</A></TD></TR>
</TABLE>
</TD>
<TD>
<?php } // end of display_head() function
function display_foot() {
?>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>
<?php } // end of display_foot() function
?>
| Users' Comments (0) |
|
No comment posted








