Templates - why and how to use them in PHP3
September 22, 2000
|
Are you faced with the task of creating a site which should be
programmed by a coder (namely you) and designed by a designer?
Need to make life easy for both of you? Sascha Schumann will
show you how to make your site customizable using PHP3s
FastTemplate!
|
Sascha Schumann
Ok, you probably want to know why you want to use FastTemplates.
- change the look of your entire web site in seconds
- abstract programming without dirty HTML
- the designer does not need to take care of all that "fuzzy" code
- it is amazingly fast
- easier reuse of old templates (think of common forms)
FastTemplate is derived from a similar named Perl packet (can be found on
CPAN). You can download the PHP port from
its homepage. You just need
one class file (class.FastTemplate.php3) from it.
Let me explain first that there is a difference between creating a page
using templates and simply spitting it out by using echo or print. The
simple echo/print approach is great for writing short scripts, but leaves
you alone with better organization or customization. Templates on the
other hand give you the ability to create multiple language sites by
just changing one parameter. They force you to pay attention to what you do.
Do not be afraid of thinking before you start coding. It may take some time,
but this time will be given back to you after your project grows in size.
So, how does one utilize FastTemplate? You need to start with a simple
call to
<?php $tpl = new FastTemplate("path"); ?>
passing it the path to the template
directory where all your template files live. It returns an object which
you can use to assign parameters, create pages and so on.
FastTemplate is based on the assumption that a large page is built out of
many small parts. Every part has a unique name. The smallest part is
a normal text string which is assigned to such a unique name. This
is done by
<?php $tpl->assign(NAME, "text"); ?>.
Now, FastTemplate knows what you mean, if one of your templates contains
{NAME}
.
Additionally, FastTemplate needs to know how you want to call your
templates. You need to give it a hint by passing an associative array to
<?php $tpl->
define();
?>
:
<?php $tpl->define(array(foo => "foo.tpl", bar => "bar.tpl")); ?>
This assigns the name foo and bar to the respective files (namely foo.tpl and bar.tpl).
Contents:
FastTemplate Substitution
Building Whole Tables
FastTemplate Substitution
|