Web Development With PHP FastTemplate (
Page 1 of 9 )
Typically, most PHP-based Web sites use scripts which contain
intermingled PHP and HTML code. While this speeds up development, it also
has a downside: an interface designer cannot modify page layouts or HTML
templates without the assistance of an experienced PHP developer. Well,
there's a solution to the problem - and you'll be surprised to hear that
it's been around for quite a while. Say hello to PHP FastTemplate.If you've worked with PHP, JSP, ASP or any other tag-based scripting language,
you already know that one of the most powerful features of these languages is
the ability to combine regular HTML code with programming constructs like
variables and function calls. By substituting variable "placeholders" for actual
content in an HTML page, these languages make it easy to construct dynamic Web
pages; simply alter the values of the variables embedded within the HTML code,
and the content displayed on the page changes appropriately.
As any
software designer will tell you, however, this convenience comes at a price -
most PHP scripts are so closely interwoven with HTML code that maintaining them
is a nightmare. Since both the HTML user interface elements and the program
logic are in the same physical file, it becomes difficult for users with no
programming experience to modify one without affecting the other. The most
common example of this is when interface designers need to alter the look and
feel of a particular Web application; typically, the changes they make to the
HTML code must be monitored by a developer to ensure the integrity of the
remainder of the script.
As you might imagine, having a developer
hand-hold an interface designer through an interface re-design process is
annoying to both parties concerned, not to mention expensive to the organization
as a whole. A simpler alternative would be to somehow separate the interface
(HTML code) from the programming routines (PHP, Perl et al), such that each
could be modified independent of the other; this would allow designers to do
what they do best, and developers to sleep nights without worrying about the
likely repercussions on their beautifully-handcrafted software
routines.
Which brings us to FastTemplate... {mospagebreak title=When
Time Is Money} FastTemplate - the PHP version - is actually a port of a
well-known Perl module. It is designed, in the author's words, to "manage
templates and perform variable interpolation", and is packaged as a single PHP
class which can be easily included in your application.
FastTemplate uses
"templates" to simplify maintenance of PHP code, and to separate data from page
elements. It assumes that a single Web application is made up of many smaller
pieces - it calls these parts "templates" - and provides an API to link
templates together, and to fill them with data.
In FastTemplate lingo, a
"template" is simply a text file, typically containing both static elements
(HTML code, ASCII text) and FastTemplate variables. When FastTemplate reads a
template file, it automatically replaces the variables within it with their
values. These values may be defined by the developer at run-time, may be read
from another file, or may be extracted from a database.
As you will see,
FastTemplate also makes it possible to "nest" one template within another,
adding a whole new level of flexibility to this template-based method of doing
things. By allowing you to split up a user interface into multiple smaller
parts, FastTemplate adds reusability to your Web application (a template can be
used again and again, even across different projects) and makes it easier to
localize the impact of a change.
Before proceeding further, you should
visit the FastTemplate home page at http://www.thewebmasters.net/ and download a
copy of the latest version (1.1.0 at the time of writing). The package contains
the main class file, a document outlining the exposed methods and variables, and
some example scripts.
FastTemplate was originally written for PHP3; if
you're using PHP4, you'll need to patch it by making a couple of modifications
to the class file. A list of the changes is available on the FastTemplate Web
site.