Are you faced with creating a website that is created by both a programmer (probably you) and a designer? Don't you know how to make the job easier for both of you? Let me give you the answer: use FastTemplate to make your site easier to customize!
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 (vs. regular form)
FastTemplate is derived from a Perl package of the same name (which 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 echoing or printing it out.
Simply using the echo/print method is great for writing short scripts, but it doesn't help you with better organization and customization. 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 apply FastTemplate? First you need to make a simple call:
<?php $tpl=new FastTemplate ("path");
?>
Pass it a path to the directory where all your template files are stored. It returns an object, which you can use for parameter assignment, page generation, 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 <?php
$tpl->assign(NAME, "text");
?>
to complete. Now, if one of your templates contains {NAME}, FastTemplate knows your intent.
Additionally, FastTemplate needs to know how you want to call your template. You need to pass an associative array (associative
array) to <?php $tpl->define(); ?>
Come give it a hint.
<?php
$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
<?php
$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 it.
{PAGECONTENT} macro. We also need to assign a value to PAGETITLE, as follows: <?php
$tpl->assign(PAGETITLE, "FooBar test");
$tpl->parse(MAIN, "bar");
?>
Easy, isn't it? We just need to output it now: <?php
$tpl->FastPrint(MAIN);
?>
The following three files show more detailed descriptions of actual exercises. I don't know how to live without this technology in real life--
Your designers will be happy and your boss will smile because you can get more done 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 -->
Clearly nothing was done. Please see {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);
?>
Creating an entire table I also wrote a short example to demonstrate how to generate an entire table from a single-row template. It's efficient because you still don't need to modify the HTML document directly.
We create an HTML table by appending the content of a template to an already defined unique name. This can be done by calling
When $tpl->parse(), add a "." before the template name to achieve this. <?php
//Assign the contents of template foo to TPL1
$tpl->parse(TPL1, "foo");
// Attach the content of template bar after 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 be saying, "That's all great. But won't it affect the speed of my site?"
No, your site will probably get faster. One simple reason: because you as a programmer care about designing your application and writing code, your code will be more efficient, handling the same tasks easier and faster. So, you might add another reason to the list of reasons listed above for why you should consider using FastTemplate in your project.
If you just want to convert an existing web site, the performance success may not be noticed. I recommend using regex buffering in PHP, it will help in this situation. Because FastTemplate uses regular expressions for every macro, each regular expression will be compiled only once and the speed impact will be negligible.