• Aucun résultat trouvé

Using require() for Website Templates

Dans le document PHP and MySQL ® Web Development (Page 175-180)

Using require() for Website Templates

If your company’s web pages have a consistent look and feel, you can use PHP to add the template and standard elements to pages using require().

For example, the website of fictional company TLA Consulting has a number of pages, all with the look and feel shown in Figure 5.2.When a new page is needed, the developer can open an existing page, cut out the existing text from the middle of the file, enter new text, and save the file under a new name.

Figure 5.2 TLA Consulting has a standard look and feel for all its web pages.

Consider this scenario:The website has been around for a while, and the company now has tens, hundreds, or maybe even thousands of pages all following a common style. A decision is made to change part of the standard look; the change might be something minor, such as adding an email address to the footer of each page or adding a single new entry to the navigation menu. Do you want to make that minor change on tens, hun-dreds, or even thousands of pages?

Directly reusing the sections of HTML common to all pages is a much better approach than cutting and pasting on tens, hundreds, or even thousands of pages.The source code for the home page (home.html) shown in Figure 5.2 is given in Listing 5.1.

Listing 5.1 home.html—The HTML That Produces TLA Consulting’s Home Page

<html>

<head>

<title>TLA Consulting Pty Ltd</title>

<style type=”text/css”>

h1 {color:white; font-size:24pt; text-align:center;

font-family:arial,sans-serif}

138 Chapter 5 Reusing Code and Writing Functions

<table width=”100%” cellpadding=”12” cellspacing=”0” border=”0”>

<tr bgcolor=”black”>

<td align=”left”><img src=”logo.gif” alt=”TLA logo” height=”70”

width=”70”></td>

<td>

<h1>TLA Consulting</h1>

</td>

<td align=”right”><img src=”logo.gif” alt=”TLA logo” height=”70”

width=”70”></td>

</tr>

</table>

<!-- menu -->

<table width=”100%” bgcolor=”white” cellpadding=”4” cellspacing=”4”>

<tr >

<td width=”25%”>

<img src=”s-logo.gif” alt=”” height=”20” width=”20”>

<span class=”menu”>Home</span></td>

<td width=”25%”>

<img src=”s-logo.gif” alt=”” height=”20” width=”20”>

<span class=”menu”>Contact</span></td>

<td width=”25%”>

<img src=”s-logo.gif” alt=”” height=”20” width=”20”>

<span class=”menu”>Services</span></td>

<td width=”25%”>

<img src=”s-logo.gif” alt=”” height=”20” width=”20”>

<span class=”menu”>Site Map</span></td>

</tr>

</table>

Listing 5.1 Continued

07_0672329166_ch05.qxd 9/3/08 1:13 PM Page 138

139 Using require() for Website Templates

<!-- page content -->

<p>Welcome to the home of TLA Consulting.

Please take some time to get to know us.</p>

<p>We specialize in serving your business needs and hope to hear from you soon.</p>

<!-- page footer -->

<table width=”100%” bgcolor=”black” cellpadding=”12” border=”0”>

<tr>

<td>

<p class=”foot”>&copy; TLA Consulting Pty Ltd.</p>

<p class=”foot”>Please see our

<a href=”legal.php”>legal information page</a></p>

</td>

</tr>

</table>

</body>

</html>

You can see in Listing 5.1 that a number of distinct sections of code exist in this file.The HTML head contains cascading style sheet (CSS) definitions used by the page.The sec-tion labeled “page header” displays the company name and logo, “menu” creates the page’s navigation bar, and “page content” is text unique to this page. Below that is the page footer.You can usefully split this file and name the parts header.php,home.php, and footer.php. Both header.phpand footer.phpcontain code that will be reused on other pages.

The file home.phpis a replacement for home.htmland contains the unique page con-tent and two require()statements shown in Listing 5.2.

Listing 5.2 home.php—The PHP That Produces TLA’s Home Page

<?php

require('header.php');

?>

<!-- page content -->

<p>Welcome to the home of TLA Consulting.

Please take some time to get to know us.</p>

<p>We specialize in serving your business needs and hope to hear from you soon.</p>

<?php

require('footer.php');

Listing 5.1 Continued

140 Chapter 5 Reusing Code and Writing Functions

The require()statements in home.phpload header.phpand footer.php.

As mentioned previously, the name given to these files does not affect how they are processed when you call them via require(). A common convention is to call the par-tial files that will end up included in other files something.inc(here,incstands for include).This is not recommended as a general strategy, as .incfiles will not be inter-preted as PHP code unless the web server has been configured specifically for this.

If you’re going to do this, you should place your include files in a directory that can be seen by your scripts but does not permit your include files to be loaded individually via the web server—that is, outside the web document tree.This setup is a good strategy because it prevents these files from being loaded individually, which would either (a) probably produce some errors if the file extension is .phpbut contains only a partial page or script, or (b) allow people to read your source code if you have used another extension.

The file header.phpcontains the CSS definitions that the page uses, the tables that display the company name, and navigation menus, as shown in Listing 5.3.

Listing 5.3 header.php—The Reusable Header for All TLA Web Pages

<html>

<table width="100%" cellpadding="12" cellspacing="0" border="0">

<tr bgcolor="black">

<td align="left"><img src="logo.gif" alt="TLA logo" height="70" width="70"></td>

<td>

<h1>TLA Consulting</h1>

</td>

07_0672329166_ch05.qxd 9/3/08 1:13 PM Page 140

141 Using require() for Website Templates

<td align="right"><img src="logo.gif" alt="TLA logo" height="70" width="70" /></td>

</tr>

</table>

<!-- menu -->

<table width="100%" bgcolor="white" cellpadding="4" cellspacing="4">

<tr >

<td width="25%">

<img src="s-logo.gif" alt="" height="20" width="20" />

<span class="menu">Home</span></td>

<td width="25%">

<img src="s-logo.gif" alt="" height="20" width="20" />

<span class="menu">Contact</span></td>

<td width="25%">

<img src="s-logo.gif" alt="" height="20" width="20" />

<span class="menu">Services</span></td>

<td width="25%">

<img src="s-logo.gif" alt="" height="20" width="20" />

<span class="menu">Site Map</span></td>

</tr>

</table>

The file footer.phpcontains the table that displays the footer at the bottom of each page.This file is shown in Listing 5.4.

Listing 5.4 footer.php— The Reusable Footer for All TLA Web Pages

<!-- page footer -->

<table width=”100%” bgcolor=”black” cellpadding=”12” border=”0”>

<tr>

<td>

<p class=”foot”>&copy; TLA Consulting Pty Ltd.</p>

<p class=”foot”>Please see our <a href=”legal.php”>

legal information page</a></p>

</td>

</tr>

</table>

</body>

</html>

Listing 5.3 Continued

142 Chapter 5 Reusing Code and Writing Functions

This approach gives you a consistent-looking website very easily, and you can make a new page in the same style by typing something like this:

<?php require(‘header.php’); ?>

Here is the content for this page

<?php require(‘footer.php’); ?>

Most importantly, even after you have created many pages using this header and footer, you can easily change the header and footer files.Whether you are making a minor text change or completely redesigning the look of the site, you need to make the change only once.You do not need to separately alter every page in the site because each page is loading in the header and footer files.

The example shown here uses only plain HTML in the body, header, and footer.This need not be the case.Within these files, you could use PHP statements to dynamically generate parts of the page.

If you want to be sure that a file will be treated as plain text or HTML, and not have any PHP executed, you may want to use readfile()instead.This function echoes the content of a file without parsing it.This can be an important safety precaution if you are using user-provided text.

Dans le document PHP and MySQL ® Web Development (Page 175-180)

Documents relatifs