Php-web application development: using template _php

Source: Internet
Author: User
Keywords templates program development application processing data replacement template
Tags php template
Program Development

The developers of each of the larger Php-web application designs probably have the following experience: Spend a lot of time writing hypertext statements, typesetting pages, creating artwork, and so on, or spending a lot of time on the integrated program code and HTML static pages. It is true that the development of Web applications in scripting languages is not easy to separate data processing and data display, but in the case of multi-person cooperation, if the data and display can not be separated, will greatly affect the efficiency of development, professional division of labor play. In order to solve this problem, PHP also provides its own solution, there are many, this article mainly introduces the template class in Phplib.

1 Design of template processing class
The following tasks are primarily required for template processing classes:
• Read the HTML code for the display from the template file.
• Combine the template file with the actual generated data to produce the results of the output.
• Allows multiple templates to be processed at the same time.
• Allow nesting of templates.
• Allow processing of a separate part of the template.

To summarize the above tasks, the template class is designed to read the displayed HTML code from multiple template files, replace them with data from PHP program operations where dynamic data is needed, and then output them in a certain order. Where the part of the replacement can be set freely.

Read the HTML code for the display using the Read file method
Combination of template files and data with regular substitution
Working with multiple templates is implemented using array storage.
Nested implementations of templates The main idea is that the template and output (any intermediate analysis results) are treated as a substitute, can be implemented.
The processing of the individual parts is accomplished by setting the callout in the template file and then controlling it with the label in the regular substitution, which implements the partial substitution.

2 implementation of the template processing class
See Template.inc in Phplib for a total of 345 lines of code with detailed comments. Some of the main functions are listed below for reference:
1) function Set_file ($handle, $filename = "") line 77, read file
2) function Set_var ($varname, $value = \ "\") line 119, set map data-replace variable
3) function Set_block ($parent, $handle, $name = \ "\") line 96, set callout
4) function subst ($handle) line 136, perform data substitution
5) function Parse ($target, $handle, $append = False) line 165, perform a combination of template file and data
6) function P ($varname) line 268, output processing result
Note: I downloaded the php-lib7.2c template.inc file in the 95th line less a "/", plus after use normal.

3 Use of template processing classes
3.1 Most basic examples
For simplicity, this assumes that the template file, the PHP file that uses the template, and the template processing class are all placed in the same directory. The custom in Phplib is to use the ihtml suffix as the suffix of the template file.
Here is the template file to use:




Test using a template


This is a test file that uses a template!


The current time is {currenttime}!


Note: The template file is almost the same as the usual HTML file, except that the variable that uses "{}" is the dynamic content that can be replaced by the template-handling class.

Next, use the template processing class to process the above template:
Introducing the template class
Include (\ "template.inc\");

Get the data you need to replace
$timeNow =date (\ "Y-m-d h:i:s\", Time ());

Instantiate a template class
$template = new template ();

Loading test.ihtml templates
$template->set_file (\ "handle1\", \ "Test.ihtml\");

Replace the currenttime in the template with the value of $timenow
$template->set_var (\ "Currenttime\", $timeNow);

Perform the actual template operation
$template->parse (\ "output\", \ "handle1\");

Output final Result
$template->p (\ "output\");
?>


Note: If you only want to use the template class in Phplib, simply include the Template.inc class in the header of the file.
When you create a template object, you can specify the path to the templates file, such as: New Template ("/htdocs/apps/templates/"), which defaults to the current path.

3.2 Template nesting and block setting
The following example comes from a reference manual with Phplib, which is more comprehensive, and it is important to note that the purpose of the set block is not related to nesting, but this example contains both. Please read carefully, block setting is to avoid this situation: Originally can be in a template file (static page) to complete the content, because of the need for a partial loop, and the partial loop content extracted separately made template file. Think, if you don't use block setting, does this example require 3 template files?
Template file 1,page.ihtml


{PageTitle}









{PageTitle}

{out} Content


Template file 2,box.ihtml










{TITLE}
{NUM} {Bignum}

Template processing file, test.php
Introducing the template class
Include (\ "template.inc\");

#实例化一个Template类, named $t.
$t = new Template ();

# Create an array that contains template files
$t->set_file (Array (
\ "Page\" = "page.ihtml\",
\ "Box\" = "box.ihtml\");

# Load template file in box with a block row, the reference name is rows
$t->set_block (\ "box\", \ "row\", \ "Rows\");

# Set Substitution
$t->set_var (Array (\ "title\" = "testpage\",
\ "Pagetitle\" = "hugo\");

# Generate Data Num,bignum
for ($i =1; $i <=3; $i + +) {
$n = $i;
$nn = $i *10;
#设置替换
$t->set_var (Array (\ "num\" and "= $n, \" bignum\ "= $nn));
#进行分析, the results of the analysis are added to the back of rows
$t->parse (\ "Rows\", \ "row\", true);
}

# Generate box, then generate page
$t->parse (\ "out\", Array (\ "box\", \ "page\"));

# Output Final Result
$t->p (\ "out\");
?>


Note: The name of the variable in the page.ihtml template file and the last output handle are used "out".
The cyclic value part adopts the database class to combine the data generation with the database application.
Execution results such as:


Template nesting and block setting execution result diagram

4 Summary
This paper briefly introduces the design, implementation and use of template class in Phplib. Of course, there are many other PHP template schemes, such as the fasttemplates evolved from Perl. Currently, the Internet team is using another scheme, the main implementation is template file storage, using Eval to achieve the combination of data and template files, relative to the template file management and template processing use is relatively concise, but the lack of file storage mode. Some of my current attempts are to combine the two and to improve on the basis of the Phplib template class. There are two initial target tasks: 1. Extend its support to the database when reading the template file, thus enhancing flexibility when necessary and combining the use of database management Tools 2, simplify template files and data. Because in practice, in most cases, you do not need to set the variables in the template file in the handler (data variable mappings) again.
In this also hope that readers, PHP program enthusiasts join in, more valuable advice, good luck!

  • 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.