PHP Template Systems - dummies

By Steve Suehring, Janet Valade

Template systems can reduce the amount of work that you need to do in PHP to make a website. When you make a website, you frequently use the same layout for the entire site. You have a top portion, maybe with a menu; a main content area; and a bottom part, maybe with links or a copyright notice.

Each and every page needs the same CSS and HTML to create this integrated look and feel throughout the website. When your website has only a couple pages, it’s probably fine to keep the HTML and CSS separate. If you need to make a change, say to add a menu item or change the copyright year, you can just edit each file.

But imagine if your website has dozens or even hundreds of pages. Now changing that copyright year or adding a menu item (or whatever) becomes quite a task. Making global changes like that, without a template, requires you to edit every file to make that change and ensure that you don’t make a mistake or typo in one of those edits.

Enter templates. A template is simply a file that contains standard or boilerplate information used to create other files. Templates are a way to reduce repeated code.

For example, you can make a top portion and a bottom portion of the page that are common among your pages. You can easily include the header and footer on each page, and then if you need to make a global change to one of these areas, you make the change only once and it applies to all the common headers or footers.

Not everything can be part of a template or is a good candidate for being a part of a template system. Areas of pages that are common across multiple pages, like the header or footer, are good candidates and can be templated easily. However, the main content area, which is typically different on every page, can’t really be templated.