Home > Article > Backend Development > PHP3 FastTemplate_PHP Tutorial
When you are building a website, you may have to face the following realities: The website requires a programmer to design the program and a website designer to organize the pages. So, is there a way to combine the two well? Yes, you can use templates (FastTemplate: this program can be found in "Programs and Code" on this site), which will make your work easier. The following explains the benefits of using templates: 1. The appearance of the entire site can be replaced in a short period of time 2. Allows programmers to abstract programming without touching HTML code 3. The speed is very fast 4. Previous templates can be reused The origin of the template: FastTemplate comes from the Perl software package of the same name (can be found on CPAN). It has been ported to the PHP3 platform. You only need a basic class file class.FastTemplate.php3. First explain the difference between using templates and using echo or print commands to create html pages. echo and print are very practical when writing short scripts, but the organization of the scripts is And the customizability is not good, and it is quite time-consuming to modify. Templates are much more efficient when writing sites that support multiple languages. For example, using echo and print, you can imagine the workload. Don't worry, using FastTemplate proficiently will take up part of your time, but this time will be made up for in your future work, especially for large projects. So, how to use FastTemplate? The first step is to just use the function , where path points to the path where the template directory is located. This function creates a $tpl object, which can later be assigned parameters, processed or used to create various pages, etc. 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, FOOT, etc. The entire page is assigned a variable name, and each small part is assigned a variable name. The smallest indivisible part is usually a string, which is also assigned a variable name. When it comes to specific processing, it is a layer-by-layer inclusion relationship. The included part appears in the previous layer as macro {NAME}. Finally, through layer-by-layer upward output, a complete page is obtained. So what is the lowest-level function that assigns values to a string? It is: assign(NAME, "text"); ?> Through this function, FastTemplate assigns the string text to the variable NAME, and then it can be moved up a level. Replace the content of macro {NAME} with text. For example: $tpl->assign(NAME, "me"); This assigns the variable NAME to the string "me". In the second step, $tpl needs to know all the template files it calls, that is, each small part. This function is implemented by defining an array: define(); ?> For example: define(array(foo => "foo.tpl", bar => "bar.tpl")); ?> This shows A total of two template files are included: foo.tpl and bar.tpl, and the names foo and bar are assigned to them. With the knowledge in the first section, do you now want to try replacing the macro {MACROS} part contained in the template file with the variables you define? This can be achieved using the following command: parse(PAGECONTENT, "foo"); ?> The specific meaning of this command is: We have first used assign to define several macro variables contained in the FOO template, and then based on these variables Replace the template file FOO, and assign the replaced template file to another variable name PAGECONTENT. It is as follows: assign(NAME, "me"); $tpl->parse(PAGECONTENT, "foo"); ?> Of course, we are 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 also contains the macro variables {PAGETITLE} and {PAGECONTENT} waiting for processing. PAGECONTENT has been obtained after processing FOO, and PAGETITLE has not been specified, so PAGETITLE must be specified and the function parse(MAIN, "bar"); ?> Process and assign the processed result to the variable MAIN. As follows: assign(PAGETITLE, "FooBar test"); $tpl->parse(MAIN, "bar"); ?> It's very simple, in the end we only need to output the page: FastPrint(MAIN) ; ?> Below are foo.tpl, bar.tpl and the final demo.php3 file.Please think about it carefully: -------------------------------------------------- ------------------ foo.tpl This does not do anything obvious. Please look at {NAME}. ------------- ----------------------------------------------------- bar. tpl