A Basic PHP Template or how I learned to love the require() command

  posted by 'Gregory Gates' on April 1, 2008 at 9:00 pm

This article reviews the basics of a template web site. Anyone that knows the history of the modern web server may remember Server Side Includes (SSI). Templates are the intelligent inclusion of smaller HTML components while rendering an HTML page. It is really basic, anyone that has done any programming knows the importance of including other libraries or components instead of writing everything from scratch. But there is flaw that server side include files introduced that is still present today.

The modern web applications are dynamic in nature. Gone is the past where a HTML page contained the entire contents of the page, Server Side Includes and databases access attests to that. But, server side includes are not dynamic in nature, the actual construct requires the named file be defined, it cannot be a variable. For example, if I am writing a blogging web site containing articles written, the HTTP request would be some thing like article.html?id=101 (or article.asp?id=101); if the article was in a database, a request of article id=101 would yield the article which could be put into the rendered HTML page by almost any web server programming language like ASP or PHP; if the article was stored in the file system named 101.html, the Server Side Include mechanism breaks down because it cannot dynamically include the 101.html file.

Microsoft's ASP include mechanism uses SSI making it flawed. PHP uses a dynamic include mechanism making it powerful. To continue the example, the construct of SSI for our example looks like this: . For every article written, the would have to be present and if conditions would prevent only the 101.html article from being included. For every article written, think about the modern online newspaper, that is millions of current and past articles requiring the article.html page be continually updated with if conditions and the SSI include like for each new article.

The PHP dynamic include mechanism is easy: include('101.html'). Or, even better: include( $_GET['id'] ). For layman, the HTTP GET variable 'id' is read making the include be whatever is requested, if the file is not present, nothing is returned and no errors generated.

Enter the template system using a dynamic naming mechanism. The entire site can use both the HTTP GET naming (article.php?id=101) or the Wiki naming (article.php?101) style allowing the PHP code to include dynamically the HTML pages requested.

Now the fun part, creating a robust web application. The Server Side Includes were the start of the modern template system where a request HTML page could be put together from smaller, reusable HTML components. The ICCA electronic commerce web site displays a credit card form in many places, along with a contact address form and other key information like the chapter or SIG to join, the member business description and even the old search key selection fields. The new ICCA web site has almost thirty of these small, includable components all containing PHP code displaying pre-filled form information wherever possible. The PHP language require() and require_once() commands make this work perfectly.

Each ICCA HTML page rendered on the new site includes a header template, a footer template, a menuing template and a body content template. The body content template includes other sub-components including banner ads, blog articles, etc. The more complicated electronic commerce page is the member application which includes a member type form, member contact form, member firm detail form, chapter to join form, SIG to join form, business description form and the much discussed credit card payment form. The less complicated webinar signup page includes a member login form (if not logged in), webinar selection form, customer contact form (pre-filled with logged in member information), and the credit card payment form.

The key here is the header and footer components. The previous ICCA web site maintained the header and footer in each of the ASP pages, there were over sixty of these page throughout the subdirectories, making changing the look and feel of the web site a tedious matter of manually changing each file; or hoping a search and replace across the entire subdirectory structure worked; or hoping that DreamWeaver or other web site tool could do this auto-magically. The new ICCA web site allows changing the header and footer portions across all pages by changing only the one sub-component called header.php.

The modern template system is not really modern, it has been around for many years, it takes a well thought-out design that builds upon reusable components in a good language that supports it, like PHP. The new ICCA web site will continually be enhanced more rapidly with this basic PHP template system.

In the end, the back-end web application is only as good as the HTML output that it creates. The classic HTML output created by earlier web sites really is not good HTML. The new ICCA web site will create good, validated HTML. The next article in this series will outline the different levels of valid HTML aimed at the new standard of XHTML that the new ICCA strives towards.
Comments No comments for this entry... Add
2009
2008
2007
2006