PHP3 FastTemplate_PHP tutorial

Source: Internet
Author: User
Tags php3 file
FastTemplate of PHP3. 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. So, whether or not you have to face the following reality when creating a site: The site requires a programmer to design a program and a website designer to organize the page. 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 explains the benefits of using templates: 1. you can replace the appearance of the entire site in a short time. 2. This allows programmers to abstract programming, without the need to access HTML code 3, the speed is very fast 4, you can reuse the history of the previous template Template: FastTemplate 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 explains 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"); this assigns the variable NAME a value to 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 to replace the macro {MACROS} in the template file with your own defined variables? Run the following command: 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: 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: Assign (PAGETITLE, "FooBar test"); $ tpl-> parse (MAIN, "bar");?> It's easy. at last, we only need to output the page: 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 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: Parse (TPL1, "foo"); # process the template bar and append it to the variable TPL1 $ tpl-> parse (TPL1, ". bar");?> The following is an example of a complete table. Feature world-{PAGE_TITLE} {PAGE_TITLE} {PAGE_CONTENT} Table. tplName Size{TABLE_ROWS} table_row.tpl {FILENAME} {FILESIZE} yad. php3 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");?> Last point on speed: After reading the above example, you will say, "it'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.

A programmer is required to design a program and a website designer to organize the page. So, are there any enable two...

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.