Are you faced with the task of creating a site which should be programmed
by a coder (namely you) and designed by a designer? Do not you know
how to make the life easy for both of you? I have got the answer for you:
Use FastTemplate to make your site more customizable!
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).
Now you want FastTemplate to substitute all your {MACROS} with their
respective values in the template
foo
Issue
the command
<?php $tpl->parse(PAGECONTENT, "foo"); ?>
to do that. This
command will assign the content of the template "foo" to the unique name
PAGECONTENT.
Of course, we are not done with that yet since the template
bar
holds the main page definition
and FastTemplate needs to substitute the macro {PAGECONTENT} in it.
We also need to assign
a PAGETITLE and to do that we say:
<?php
$tpl->assign(PAGETITLE, "FooBar test");
$tpl->parse(MAIN, "bar");
?>
Easy, is it not? We just need to print it out now:
<?php
$tpl->FastPrint(MAIN);
?>
The following three files show how the more verbose description
looks in practice. I would not know how to live without this
technique in real life - your designers will be happy and your boss
will smile, because you can do more things in less time.
bar.tpl
<!-- bar.tpl -->
<HTML>
<HEAD><TITLE>Feature world - {PAGETITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGETITLE}</H1>
{PAGECONTENT}
</BODY>
</HTML>
foo.tpl
<!-- foo.tpl -->
This does not do anything obvious. Please look at {NAME}.
demo.php3
<?php
include "class.FastTemplate.php3";
$tpl = new FastTemplate(".");
$tpl->define(array(foo => "foo.tpl", bar => "bar.tpl"));
$tpl->assign(NAME, "me");
$tpl->assign(PAGETITLE, "Welcome!");
$tpl->parse(PAGECONTENT, "foo");
$tpl->parse(MAIN, "bar");
$tpl->FastPrint(MAIN);
?>
Building whole tables
I have also written a short example which demonstrates how to build
whole tables with single row templates. It is effective, because
you still do not need to tinker with HTML directly.
We attach the content of a template to an
already defined unique name to build the HTML table. This can be
done by sticking a single "." into the front of the template name
when calling $tpl->parse():
<?php
# assign the content of template foo to the unique name TPL1
$tpl->parse(TPL1, "foo");
# attach the content of template bar to the unique name TPL1
$tpl->parse(TPL1, ".bar");
?>
page.tpl
<HTML>
<HEAD><TITLE>Feature world - {PAGE_TITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGE_TITLE}</H1>
{PAGE_CONTENT}
</BODY>
</HTML>
table.tpl
<TABLE>
<TR> <TH>name</TH> <TH>size</TH> </TR>
{TABLE_ROWS}
</TABLE>
table_row.tpl
<TR>
<TD>{FILENAME}</TD>
<TD>{FILESIZE}</TD>
</TR>
yad.php3
<?php
include "class.FastTemplate.php3";
function InitializeTemplates() {
global $tpl;
$tpl = new FastTemplate(".");
$tpl->define( array( page => "page.tpl",
table => "table.tpl",
table_row => "table_row.tpl" ) );
}
function ReadCurrentDirectory() {
global $tpl;
$handle = opendir(".");
while($filename = readdir($handle)) {
$tpl->assign(FILENAME, $filename);
$tpl->assign(FILESIZE, filesize($filename));
$tpl->parse(TABLE_ROWS, ".table_row");
}
closedir($handle);
$tpl->parse(PAGE_CONTENT, "table");
}
function PrintPage($title) {
global $tpl;
$tpl->assign(PAGE_TITLE, $title);
$tpl->parse(FINAL, "page");
$tpl->FastPrint(FINAL);
}
InitializeTemplates();
ReadCurrentDirectory();
Printpage("Yet Another Demo");
?>
Speed Discussion
"Ok," you might say, "that is all fine and nifty. But doesn't it impact
the speed of my web site?"
Yes, your site will probably become faster. There is a simple reason
for that: Because you as the coder are focused on designing your
application and building code, your code will be more efficient, handling
the same task easier and quicker. So, you might add just another reason
to the above list why you should consider using FastTemplate in your
next PHP project.
If you just want to convert an existing web site, the performance hit
will probably not be noticed. I introduced a regex (short for "regular expression")
cache into PHP 3.0.7 which helps in this special case. Because FastTemplate
uses a regex for every macro, each regex will be compiled only once and
the speed impact becomes negligible.