When you build a site, you may have to face the following realities:
The site needs a programmer to design the program and a web designer to organize the pages. So, is there a way to combine the two well together?
There is the use of templates (fasttemplate: This program in the "Program and code" can be found in the site), which will make your work easier.
The following explains the benefits of using templates:
1, in a short period of time can replace the entire site's appearance
2, so that programmers can abstract programming, without touching the HTML code
3, the speed is very fast
4, can reuse the previous template
The origin of the template: Fasttemplate from the same name as the Perl package (found on CPAN). It was ported to the PHP3 platform. You only need a basic class file. Fasttemplate.php3
To explain the difference between using a template and using the Echo or Print command to create an HTML page, echo and print are very useful when writing short scripts, but the organization and customization of the scripts is not good, and the changes are quite useless. Templates are much more efficient when writing sites that support multiple languages, such as using echo and print to visualize the workload.
Do not worry, skilled use of fasttemplate will take up part of your time, but these times will be in your future work to make up for back, especially when the big project.
So, how do you use Fasttemplate?
The first step is simply to use the function , where path points to the path where the template directory resides. This function establishes a $tpl object that can then be assigned parameters, processed or used to create various pages, and so on.
Fasttemplate is based on a theory that assumes that a Web page is made up of many small parts. For example, a Web page is subdivided into title, page, foot, and so on. The entire page is given a variable name, and each part is given a variable name, and the smallest indivisible part is usually a string, which is also given a variable name. When dealing with the concrete, it is a layer of the containment relationship. The included part appears as a macro {NAME} in the previous layer. Finally, through a layer of upward output, get the complete page.
So what is the bottom-level function that assigns a value to a string, which is:
Assign (NAME, "text");?>
With this function, Fasttemplate assigns the string text to the variable NAME, and the next layer replaces the contents of the macro {NAME} with text.
For example:
$tpl->assign (NAME, "Me");
This assigns the variable name to the string "Me".
In the second step, $tpl need to know all the template files that they call, that is, the small parts. This feature is implemented by an array of define:
define ();?>
For example:
Define (Array (foo = "Foo.tpl", bar = "Bar.tpl");?>
This means that a total of two template files are included: Foo.tpl and BAR.TPL, and they are assigned the name Foo and bar.
With the knowledge of the first section, do you want to try now? The macros contained in the template file
{MACROS} parts are replaced by their own defined variables? Use the following command to achieve:
$tpl->parse (pagecontent, "foo");
?>
The specific meaning of this command is:
We have first defined some of the macro variables contained in the Foo template with assign, then replaced the template file foo with these variables and assigned the replacement template file to another variable name, PageContent.
Complete as follows:
$tpl->assign (NAME, "Me");
$tpl->parse (pagecontent, "foo");
?>
Of course, we have not finished, because the bar template file is the main output part of the Web, the bar template contains the Foo template, bar also contains the macro variable {pagetitle} and {pagecontent} wait for processing, PageContent has been processed for Foo and pagetitle has not been specified, so we also need to specify PageTitle, and call the function
Parse (MAIN, "Bar");?>
Processing and assigns the processed result to the variable main.
As follows:
$tpl->assign (pagetitle, "FooBar test");
$tpl->parse (MAIN, "Bar");
?>
Very simple, and finally we just output the page:
$tpl->fastprint (MAIN);
?>
Here is the FOO.TPL,BAR.TPL and the final demo.php3 file.
Please carefully ponder:
-------------------------------------------------------------
Foo.tpl
This does does anything obvious. Please look at {NAME}.
-------------------------------------------------------------
Bar.tpl
<title>Feature world-{pagetitle}</title>
{PageTitle}
{PageContent}
------------------------------------------------------------
Demo.php3
Include "class. Fasttemplate.php3 ";
$TPL = new Fasttemplate (".");
$TPL->define (Array (foo = "Foo.tpl", bar = "Bar.tpl"));
$tpl->assign (NAME, "Me");
$tpl->parse (pagecontent, "foo");
$tpl->assign (pagetitle, "welcome!");
$tpl->parse (MAIN, "Bar");
$tpl->fastprint (MAIN);
?>
------------------------------------------------------------
Example of compiling a table:
After the above elaboration, whether people have understood a bit.
Here is an example of working with a table, first of all we will learn some new knowledge.
When we finish processing the Foo template and give the variable TPL1, we can append the bar template content to the TPL1, so that we do not have to define too many variables, it is easy to understand, the example after processing the title of the page to append the content, and finally append foot, Generate a full page and then output. This command is:
Parse (TPL1, ". Bar");?>
One of the. Represents append.
As follows:
# Process the template foo and assign the variable TPL1
$tpl->parse (TPL1, "foo");
# Process the template bar and append the input variable TPL1
$tpl->parse (TPL1, ". Bar");
?>
Here is a complete example of the table, we have a good look and feel
Page.tpl
<title>Feature world-{page_title}</title>
{Page_title}
{Page_content}
Table.tpl
Table_row.tpl
{FILENAME}
{FILESIZE}
Yad.php3
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");
?>
The last point of the discussion on speed:
After reading the above example, you will say "Great!" Pretty, but what about the speed? ”
No problem, your site will become very fast. Simply put: Because you are a programmer, you should focus on the design of the program code, the code should be more efficient, it should be easy to modify and easy to understand. Using Fasttemplate can help you do this, so it makes your work a little easier.
If you want to replace an already built web site, we recommend replacing it with a regex (substitution expression), which in fact Fasttemplate uses a regex to replace the macros in the template.