![]() Join Up! 96822 members and counting! |
|
|||
Templates - Why and How To Use Them In PHP3
Sascha Schumann
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.
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"); ?>
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"); ?>
{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
<?php $tpl->parse(PAGECONTENT, "foo"); ?>
Of course, we are not done with that yet since the template
bar
Easy, is it not? We just need to print it out now:
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 -->
foo.tpl
<!-- foo.tpl -->demo.php3
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():
page.tpl
<HTML>table.tpl
<TABLE>table_row.tpl
<TR>yad.php3
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.
|