Entry
How do I build dynamic websites with PHP and templates?
Feb 29th, 2000 12:14
Matt Gregory, Jeff Wilcox, Dave Cross, Nathan Wallace, unknown unknown,
Quick and Easy Approach
====-------------------
The best way I have found of creating templates is to go ahead and
design your template in regular html. Then place the templates in a
common header file included in all your scripts. I usually use
commonHeader and commonFooter functions to fill in the templates on
each page. This model can be expanded to include any extra
functionality that you might need, including class-based page layout
and image-arrays for the links etc. etc.
Advanced, Extensible Model
====----------------------
When creating a truly dynamic, extensible website, you may find that
pages including and running basic header/footer functions is not
advanced enough.
A large project which would pay off once your site begins to grow would
be to create a set of classes to manage your entire web site and its'
organization. Your classes for your website would manage the
properties of your pages, how they should be displayed, what types of
content they should include, how they should be organized, etc.
Using some of the methods found in previous PHPBuilder Articles
(http://www.phpbuilder.com/), you could create an application processor
for your website.
For example, a basic script called App could be in charge of loading
most of the pages on your site, displaying them according to their
properties, and including content from multiple sources. Using the
$REQUEST_URI Apache value, you could parse the URL the page is at.
Examples:
/App/Home/
The Application loader (App) parses the REQUEST_URI, determines that
the browser is trying to access an application called Home, and loads
the 'Home' mini-application's PHP object and involks it.
/App/Companies/ABC/
This time, the object Companies is informed that a sub-application,
ABC, has been called. Then it's up to the Companies object to either
display the data regarding ABC, query a database, or load the correct
content.
I am working on a detailed reference for creating extensive dynamic
sites with PHP, and hopefully that reference, when completed, will be
easier to understand. I welcome any email questions you may have
regarding this.
Warning:
===-----------------
When using the class based method which depends on the REQUEST_URI your
site may not be portable to other web server platforms.
Also beware class-based website creation for parse-time costs. The
bigger your application for serving your pages the bigger the box
required to serve it in a decent amount of time.
I have found that having your template removed from your page-
generation scripts allows you to have a little more efficient code
reuse and signifigantly faster parse times (since you only have to
parse the generator script specific to the page you are calling).