PHP3 's fasttemplate_php Tutorial

Source: Internet
Author: User
Tags php3 file
When you build a site, you may have to face the fact that the site needs a programmer to design the program and a website 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 advantages of using templates: 1, in a short period of time can replace the entire site 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 explained 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");?> by 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 (the array (foo = "Foo.tpl", bar = "Bar.tpl")),?> This description includes a total of two template files: Foo.tpl and BAR.TPL, and assigns them the name Foo and bar. With the knowledge of the first section, do you want to try it now? Replace the macro {MACROS} part of the template file with your own defined variables? Use the following command to achieve: Parse (pagecontent, "foo");?> the specific meaning of this command is that we have first defined some of the macro variables contained in the Foo template with assign, and then replace the template file foo with these variables. and assigns the replacement template file to another variable name, PageContent. Complete as follows: Assign (NAME, "Me"); $tpl->parse (pagecontent, "foo");?> of course, we're not done yet, because the bar template file is the main output part of the Web, the bar template contains the Foo template, and the bar contains the macro variable {PageTitle} and { PageContent} wait for processing, pagecontent after the processing of Foo has been obtained, PageTitle has not been specified, so also to specify PageTitle, and call the function Parse (MAIN, "Bar"),?> processing, and assigns the processed result to the variable MAIN. As follows: Assign (PageTitle, "FooBar test"); $tpl->parse (MAIN, "Bar");?> is simple, and in the end we just output the page: Fastprint (MAIN);?> below is 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 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);?>------------------------------------------------------------to compile an example of a table: after the above elaboration, Do you have any idea? 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");?>. Represents an append. As follows: Parse (TPL1, "foo"); # Process the template bar and append the variable TPL1 $tpl->parse (TPL1, ". Bar");?> below is a complete example of a table, we have a good look and feel page.tpl <title>Feature world-{page_title}</title>

{Page_title}

{Page_content} Table.tplName Size{table_rows} table_row.tpl {FILENAME} {FILESIZE} yad.php3 Define (page = "page.tpl", table = "Table.tpl", Table_row = "Table_row.tpl"); } function Readcurrentdirectory () {global $tpl; $handle = Opendir ("."); while ($filename = Readdir ($handle)) {$TPL->a Ssign (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");?> 's final discussion on speed: After reading the above example, you would 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.

http://www.bkjia.com/PHPjc/531796.html www.bkjia.com true http://www.bkjia.com/PHPjc/531796.html techarticle when you build a site, you may have to face the fact that the site needs a programmer to design the program and a website designer to organize the pages. So, whether there is making 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.