OK, you might be wondering why you are using FastTemplates.
·Can change the appearance of your entire site in seconds
·Abstract programming, no junk HTML code
·Designers don't need to care about all "fuzzy" code
·Amazingly fast
·Easier to reuse old templates (for ordinary forms)
FastTemplate originates 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: /downloads/). You only need one of the classes' files().
Let me first explain the difference between using a template to generate a page and simply using echo or print to output the page.
The simple echo/print method is very suitable for writing short scripts, but it won't help you organize and customize better. Template on the other hand gives
You can create multilingual sites with just changing one parameter. They can make you care more about what you are going to do.
Don't be afraid to think before you start coding. It may take some time, but these expenses will pay off for you as the project develops.
So, how to apply FastTemplate? First you need to make a simple call:
<?php $tpl=new FastTemplate ("path"); ?>
Pass it a path, which is the directory where all your template files are stored. It returns an object that you can use to parameterize
Assign numbers, generate pages, etc.
FastTemplate is based on the assumption that a large page is composed of many small parts. Each part has a unique one
The name. The smallest part is to assign a normal text string like this with a unique name. This can be passed
<?php
$tpl->assign(NAME, "text");
?>
Come and finish. Now, if one of your templates contains {NAME}, FastTemplate will know you
The intention is already there.
Also, FastTemplate needs to know how you want to call your template. You need to pass an associated array (associative
array) Give <?php $tpl->define(); ?>
Let me give it a prompt.
The following is the quoted content:
<?php
$tpl->define(array(foo => "",
bar => ""));
?>
These assignments will give foo and bar different files (names and) respectively.
Now you want FastTemplate to replace all {MACROS} in template foo to the corresponding value. By issuing a command
The following is the quoted content:
<?php
$tpl->parse(PAGECONTENT, "foo");
?>
To achieve it. This command will assign the content of the template "foo" to PAGECONTENT. Of course, we have not finished it yet, because the template bar is the main page definition, FastTemplate needs to replace it.
{PAGECONTENT} macro. We also need to assign values to PAGETITLE, as follows:
The following is the quoted content:
<?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 documents show more detailed descriptions in the actual exercise. I don't know how to live without this technology in real life. --
Your designer will be happy and your boss will smile because you can do more in a shorter time.
The following is the quoted content:
<!-- -->
<HTML>
<HEAD><TITLE>Feature world - {PAGETITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGETITLE}</H1>
{PAGECONTENT}
</BODY>
</HTML>
<!-- -->
It's obvious that nothing has been done. Please see {NAME}.
The following is the quoted content:
demo.php3
<?php
include ".php3";
$tpl = new FastTemplate( ".");
$tpl->define(array(foo => "", bar => ""));
$tpl->assign(NAME, "me");
$tpl->assign(PAGETITLE, "Welcome!");
$tpl->parse(PAGECONTENT, "foo");
$tpl->parse(MAIN, "bar");
$tpl->FastPrint(MAIN);
?>
Create the entire table
I also wrote a short example to demonstrate how to generate an entire table with a single line template. It works because you still don't need to be straight
Continue to modify the HTML document.
We add the contents of a template to the back of a uniquely defined name to create the HTML table. This can be called by
When $tpl->parse(), add a "." before the template name to achieve it. <?php
// Assign the content of template foo to TPL1
$tpl->parse(TPL1, "foo");
// Attach the content of the template bar after TPL1
$tpl->parse(TPL1, ".bar");
?>
The following is the quoted content:
<HTML>
<HEAD><TITLE>Feature world - {PAGE_TITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGE_TITLE}</H1>
{PAGE_CONTENT}
</BODY>
</HTML>
The following is the quoted content:
<TABLE>
<TR> <TH>name</TH> <TH>size</TH> </TR>
{TABLE_ROWS}
</TABLE>
table_row.tpl
The following is the quoted content:
<TR>
<TD>{FILENAME}</TD>
<TD>{FILESIZE}</TD>
</TR>
yad.php3
The following is the quoted content:
<?php
include ".php3";
function InitializeTemplates() {
global $tpl;
$tpl = new FastTemplate( ".");
$tpl->define( array( page => "",
table => "",
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, "Everything is so good. But won't it affect the speed of my website?" www~
No, your website may get faster. One simple reason is: because as a programmer, you care about designing your application and writing code, your code will be more efficient and easier and faster to handle the same tasks. So, you might add another reason to the list of reasons why you consider using FastTemplate in your project.
If you just want to convert an existing web site, performance success may not be noticed. I recommend using regex buffering in PHP, it will help in this case. Because FastTemplate uses regular expressions for each macro, each regular expression will be compiled only once and the effect on speed is negligible.
·Can change the appearance of your entire site in seconds
·Abstract programming, no junk HTML code
·Designers don't need to care about all "fuzzy" code
·Amazingly fast
·Easier to reuse old templates (for ordinary forms)
FastTemplate originates 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: /downloads/). You only need one of the classes' files().
Let me first explain the difference between using a template to generate a page and simply using echo or print to output the page.
The simple echo/print method is very suitable for writing short scripts, but it won't help you organize and customize better. Template on the other hand gives
You can create multilingual sites with just changing one parameter. They can make you care more about what you are going to do.
Don't be afraid to think before you start coding. It may take some time, but these expenses will pay off for you as the project develops.
So, how to apply FastTemplate? First you need to make a simple call:
<?php $tpl=new FastTemplate ("path"); ?>
Pass it a path, which is the directory where all your template files are stored. It returns an object that you can use to parameterize
Assign numbers, generate pages, etc.
FastTemplate is based on the assumption that a large page is composed of many small parts. Each part has a unique one
The name. The smallest part is to assign a normal text string like this with a unique name. This can be passed
<?php
$tpl->assign(NAME, "text");
?>
Come and finish. Now, if one of your templates contains {NAME}, FastTemplate will know you
The intention is already there.
Also, FastTemplate needs to know how you want to call your template. You need to pass an associated array (associative
array) Give <?php $tpl->define(); ?>
Let me give it a prompt.
The following is the quoted content:
<?php
$tpl->define(array(foo => "",
bar => ""));
?>
These assignments will give foo and bar different files (names and) respectively.
Now you want FastTemplate to replace all {MACROS} in template foo to the corresponding value. By issuing a command
The following is the quoted content:
<?php
$tpl->parse(PAGECONTENT, "foo");
?>
To achieve it. This command will assign the content of the template "foo" to PAGECONTENT. Of course, we have not finished it yet, because the template bar is the main page definition, FastTemplate needs to replace it.
{PAGECONTENT} macro. We also need to assign values to PAGETITLE, as follows:
The following is the quoted content:
<?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 documents show more detailed descriptions in the actual exercise. I don't know how to live without this technology in real life. --
Your designer will be happy and your boss will smile because you can do more in a shorter time.
The following is the quoted content:
<!-- -->
<HTML>
<HEAD><TITLE>Feature world - {PAGETITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGETITLE}</H1>
{PAGECONTENT}
</BODY>
</HTML>
<!-- -->
It's obvious that nothing has been done. Please see {NAME}.
The following is the quoted content:
demo.php3
<?php
include ".php3";
$tpl = new FastTemplate( ".");
$tpl->define(array(foo => "", bar => ""));
$tpl->assign(NAME, "me");
$tpl->assign(PAGETITLE, "Welcome!");
$tpl->parse(PAGECONTENT, "foo");
$tpl->parse(MAIN, "bar");
$tpl->FastPrint(MAIN);
?>
Create the entire table
I also wrote a short example to demonstrate how to generate an entire table with a single line template. It works because you still don't need to be straight
Continue to modify the HTML document.
We add the contents of a template to the back of a uniquely defined name to create the HTML table. This can be called by
When $tpl->parse(), add a "." before the template name to achieve it. <?php
// Assign the content of template foo to TPL1
$tpl->parse(TPL1, "foo");
// Attach the content of the template bar after TPL1
$tpl->parse(TPL1, ".bar");
?>
The following is the quoted content:
<HTML>
<HEAD><TITLE>Feature world - {PAGE_TITLE}</TITLE></HEAD>
<BODY BGCOLOR=BLACK TEXT=WHITE>
<H1>{PAGE_TITLE}</H1>
{PAGE_CONTENT}
</BODY>
</HTML>
The following is the quoted content:
<TABLE>
<TR> <TH>name</TH> <TH>size</TH> </TR>
{TABLE_ROWS}
</TABLE>
table_row.tpl
The following is the quoted content:
<TR>
<TD>{FILENAME}</TD>
<TD>{FILESIZE}</TD>
</TR>
yad.php3
The following is the quoted content:
<?php
include ".php3";
function InitializeTemplates() {
global $tpl;
$tpl = new FastTemplate( ".");
$tpl->define( array( page => "",
table => "",
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, "Everything is so good. But won't it affect the speed of my website?" www~
No, your website may get faster. One simple reason is: because as a programmer, you care about designing your application and writing code, your code will be more efficient and easier and faster to handle the same tasks. So, you might add another reason to the list of reasons why you consider using FastTemplate in your project.
If you just want to convert an existing web site, performance success may not be noticed. I recommend using regex buffering in PHP, it will help in this case. Because FastTemplate uses regular expressions for each macro, each regular expression will be compiled only once and the effect on speed is negligible.