Web site templates

See more about:

Where are we?

Our first PHP task is an easy one, but one of the most important uses of PHP.

Most Web pages have regions, like this:

Web page regions

Figure 1. Web page regions

Different parts of a page are put into the regions:

Using the regions

Figure 2. Using the regions

All of the pages on a site usually have the same structure. Some regions have the same content. For example, every page might have the same thing in the bottom region. CoreDogs is like this. No matter what page you look at, the bottom is the same.

Well, almost. The message on the right of the footer is chosen randomly for each page.

Suppose you have a Web site with, say, 200 pages. Every page has this in the bottom region:

Original footer

Figure 3. Original footer

Your client (or employer, school, sister, whoever) wants to change it to this, on every page:

New footer

Figure 4. New footer

If each page is a separate HTML, that means you have to change 200 files. Ack! What a pain.

But if you use PHP to set up a template for the site, you can change all 200 pages by just changing one file. You read that right – change the entire site by editing one file!

Talk about a productivity win.

If you use just one thing from this book, make it this one: creating active Web templates with PHP. That’s what you’ll learn in this chapter. And it’s one of the easiest things you can do with PHP.

This chapter’s goals

By the end of this chapter, you should:

  • Know how to output HTML with PHP.
  • Be able to insert files inside other files.
  • Know how to create a PHP-based Web site template.
  • Understand why this is such a big win.

One thing to keep in mind: this can get confusing! Not so much because the PHP for Web templates is complex. It’s not. It’s simple, in fact.

The problem is that there are so many pieces to a Web site. HTML files, CSS files, JavaScript files, link tags, image tags, and other things. They all need special treatment.

For this chapter to make sense, you’ll need to know how Web sites are put together. I’ll assume that you’ve worked through the ClientCore book, and more-or-less understand it. You should know about images, links, nav bars, CSS files, JavaScript files, a little jQuery, and page layouts.

Let’s get started!


How to...