FastTemplate of PHP3

Source: Internet
Author: User
Tags php3 file
When you create a website, you may have to face the following reality: The site requires a programmer to design programs and a website designer to organize pages. Is there a good combination of the two? Some, that is, using the template (FastTemplate: This program can be found in the "program and code" on this site), which will make your work more effective when you create a site, you may have to face the following issues:
Websites require a programmer to design programs and a website designer to organize pages. Is there a good combination of the two?

Some, that is, use the template (FastTemplate: This program can be found in the "program and code" on this site), which will make your work easier.

The following describes the benefits of using a template:

1. you can replace the appearance of the entire site in a short time.
2. enable programmers to abstract programming without having to access HTML code
3. fast
4. you can reuse the previous template.

Template origin: FastTemplate comes from the Perl software package with the same name (which can be found on CPAN ). It was transplanted to the PHP3 platform. You only need a base class file class. FastTemplate. php3

First, 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 in writing short scripts, however, the organization and customization of the scripts made are not good, and it takes a lot of effort to modify them. templates are much more efficient in writing websites that support multiple languages, for example, echo and print can be used to imagine a huge workload.

Don't worry. using FastTemplate will take part of your time, but these times will be compensated in your future work, especially in big projects.

So, how to use FastTemplate?

Step 1: Use the function Path points to the path of the template directory. This function creates a $ tpl object. in the future, you can specify parameters for it, process them, or create various pages.

FastTemplate is based on the theory that a web page is composed of many small parts. For example, a web PAGE is subdivided into TITLE, PAGE, and FOOT. The entire page is assigned a variable name, and each small part is assigned a variable name. The smallest and inseparable part is usually a string, and it is also assigned a variable name. The specific processing is a layer-by-layer inclusion relationship. The contained part is displayed in the form of macro {NAME} in the previous layer. Finally, the complete page is displayed through a layer-by-layer output.

So what is the underlying function for assigning values to strings:

Assign (NAME, "text");?>

Through this function, FastTemplate assigns the string text to the variable NAME, and the macro {NAME} content can be replaced with text at the next layer.

For example:

$ Tpl-> assign (NAME, "me ");

The variable NAME is assigned the string "me ".

Step 2: $ tpl needs to know all the template files it calls, that is, each small part. This function is implemented by define an array:

Define ();?>

For example:

Define (array (foo => "foo. tpl", bar => "bar. tpl");?>

This description includes two template files: foo. tpl and bar. tpl, and the names foo and bar are specified for them.

With the first knowledge, do you want to try the macros contained in the template file?
What about replacing the {MACROS} part with the defined variables? Run the following command:


$ Tpl-> parse (PAGECONTENT, "foo ");

?>

The specific significance of this command is:

We have first defined several macro variables contained in the FOO Template with assign, and then replaced the template file FOO with these variables, and assign the replaced template file to another variable name PAGECONTENT.

Complete:


$ Tpl-> assign (NAME, "me ");
$ Tpl-> parse (PAGECONTENT, "foo ");

?>

Of course, we haven't finished it yet, because the bar template file is the main output part of the WEB. the BAR template contains the FOO Template, the BAR also contains macro variables {PAGETITLE} and {PAGECONTENT} waiting for processing. after PAGECONTENT is processed for FOO, PAGETITLE has not been specified. Therefore, you must specify PAGETITLE and call the function.

Parse (MAIN, "bar");?>

And assign the processed result to the variable MAIN.

As follows:


$ Tpl-> assign (PAGETITLE, "FooBar test ");
$ Tpl-> parse (MAIN, "bar ");

?>

It's easy. at last, we only need to output the page:


$ Tpl-> FastPrint (MAIN );

?>

The following is the foo. tpl, bar. tpl and the final demo. php3 file.
Please carefully consider:

-------------------------------------------------------------
Foo. tpl


This does not do anything obvious. Please look at {NAME }.

-------------------------------------------------------------
Bar. tpl



Feature world-{PAGETITLE}

{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 preparing a table:

After the above explanation, do you understand it.
The following is an example of table processing. First, we will learn some new knowledge.

After processing the foo Template and assigning the variable TPL1, we can append the content of the bar template to TPL1, so that we do not need to define too many variables and are easy to understand, for example, after processing the title of the page, append the content part, and then append the foot to generate a complete page and then output it. This command is:
Parse (TPL1, ". bar");?>
"." Indicates append.

As follows:


# Process the template foo and assign the variable TPL1
$ Tpl-> parse (TPL1, "foo ");

# Process the template bar and append it to variable TPL1
$ Tpl-> parse (TPL1, ". bar ");

?>

The following is an example of a complete table.

Page. tpl


Feature world-{PAGE_TITLE}

{PAGE_TITLE}
{PAGE_CONTENT}



Table. tpl




{TABLE_ROWS}
Name Size


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 about speed is as follows:

After reading the above example, you will say, "that's great! Pretty, but how is the speed ?"

No problem. your site will become very fast. Simply put: because you are a programmer, you should focus on the design of program code, the code should be more efficient, easy to modify and easy to understand. Using FastTemplate can help you do this, so it makes your work easier.

If you want to replace a created Web site, we recommend that you use regex (replace expression) to replace it. In fact, FastTemplate uses regex to replace the macro in the template.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.