Okay, you might be wondering why you should use FastTemplates.
·Can change the look of your entire site in seconds
·Abstract programming, no junk HTML code
·Designers don't need to care about all the "obscure" code
· Surprisingly fast
·Easier to reuse old templates (for regular forms)
FastTemplate is derived from a Perl package with the same name (can be found on CPAN). You can download the PHP version from its homepage (the download address of this site is: http://www.phpe.net/downloads/1.shtml). You only need one of the class files (class.FastTemplate.php).
Let me first explain the difference between using a template to generate a page and simply outputting the page through echo or print.
Simply using the echo/print method is great for writing short scripts, but it doesn’t help you organize and customize it better. Templates on the other hand give you
the ability to create multilingual sites just by changing a parameter. They can push you to care more about what you have to do.
Don’t be afraid to think before you start coding. It may take some time, but the cost will pay off as the project grows.
So, how to use FastTemplate? First you need to make a simple call:
Pass it a path, which is all your The directory where template files are stored. It returns an object, which you can use to assign parameters, generate pages, etc.
FastTemplate is based on the assumption that a large page is composed of many small parts. Each part has a unique
name. The smallest part is assigning it to a normal text string with a unique name. This can be done via
$tpl->assign(NAME, "text");
?>
. Now, if one of your templates contains {NAME}, FastTemplate knows what you
meant.
In addition, FastTemplate needs to know how you want to call your template. You need to give it a hint by passing an associative
array to define(); ?>
.
The following is the quoted content:
$tpl->define(array(foo => "foo.tpl",
bar => "bar.tpl" ));
?>
These assignments will give foo and bar respectively different files (named foo.tpl and bar.tpl).
Now you want FastTemplate to replace all {MACROS} in template foo with their corresponding values. By issuing the command
The following is the quoted content:
$tpl->parse(PAGECONTENT, "foo");
?>
to achieve. This command will assign the contents of template "foo" to PAGECONTENT. Of course, we are not done yet, because the template bar is the main page definition, and FastTemplate needs to replace the
{PAGECONTENT} macro. We also need to assign a value to PAGETITLE, as follows:
The following is the quoted content:
$tpl->assign(PAGETITLE, "FooBar test");
$tpl ->parse(MAIN, "bar");
?>
Easy, isn't it? We just need to output it now: $tpl->FastPrint(MAIN);
?>
The following three files show more detailed descriptions of the actual exercise. I don't know how I would live without this technology in real life --
Your designers will be happy and your boss will smile because you can do more in less time.
The following is the quoted content:
bar.tpl
< ;HEAD>