Home >Backend Development >PHP Tutorial >PHP3 FastTemplate_PHP Tutorial

PHP3 FastTemplate_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:28:071009browse

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

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 form: After the above explanation, do you understand a little bit. The following is an example of processing a table. First, let's learn some new knowledge. After we have processed the foo template and assigned it to the variable TPL1, we can process the content of the bar template and append it to TPL1. This way we do not have to define too many variables and it is easy to understand. For example, after processing the title of the page, add the content Append part of it, and finally append the foot to generate a complete page and then output it. This command is: parse(TPL1, ".bar"); ?> The . means append. As follows: parse(TPL1, "foo"); # Process the template bar and append it to the variable TPL1 $tpl->parse(TPL1, ".bar"); ?> The following is a complete table example. Hello everyone. Guess page.tpl Feature world - {PAGE_TITLE}

{PAGE_TITLE}

{PAGE_CONTENT} table.tpl name 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"); ?> A final discussion about speed: After reading the above examples, you will say "Awesome! Beautiful, but, speed How about it?" No problem, your site will become faster. 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 job a little easier. If you want to replace an already built website, we recommend using regex (replacement expression) to replace it. In fact, FastTemplate uses regex to replace macros in the template.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531796.htmlTechArticleWhen you are building a website, you may have to face the following realities: The website requires a programmer. Design program and a website designer to organize the pages. So, is there any way to make two...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn