Php-web application Development: using templates

Source: Internet
Author: User
Tags array contains current time final include reference
web| Program | template

Each developer with a larger php-web application design probably has the following experience: Spend a lot of time writing hypertext statements, composing pages, making artwork, and so on, or spending a lot of time on consolidated program code in and HTML static pages. Indeed, the use of scripting language to develop WEB applications is not easy to separate data processing and data display, but in the case of many people cooperation, if the data and display can not be separated, will greatly affect the efficiency of development, professional division of the play. To solve this problem, PHP also provides its own solution, there are many, this article mainly introduces the Template class in Phplib.

Design of template Processing class
Template processing classes need to accomplish the following tasks:

    • Reads the displayed HTML code 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.
    • Allows nesting of templates.
    • Allows processing of a separate part of the template.



To sum up the above tasks, the design goal of the template class is to read the displayed HTML code from multiple template files, replace the dynamic Data in the display code with the data obtained by the PHP program operation, and then output it in a certain order. In which, the replacement part can be set freely.

Read the displayed HTML code in the form of a read file
Combination of template files and data using regular replacements
Processing multiple templates is implemented using array storage.
The main idea of nesting of templates is that the template and output (any intermediate analysis results) are treated equally, and can be replaced.
The processing of the individual part is done by setting the callout in the template file and then combining the annotation in the regular substitution to control the partial substitution.

Implementation of template Processing class
Refer to the Template.inc in Phplib, a total of 345 lines of code, with detailed comments. The following are some of the main functions for studying the reference:
1 function set_file ($handle, $filename = "") line 77, reading file
2 function Set_var ($varname, $value = "") line 119, set mapping data-replace variable
3 function Set_block ($parent, $handle, $name = "") line 96, set callout
4 function subst ($handle) line 136, performing data substitution
5 function Parse ($target, $handle, $append = False) line 165, performing template file and data binding
6 function P ($varname) line 268, output processing results
Note: I downloaded the php-lib7.2c template.inc file in the 95th line less "/", plus after the use of normal.

Third, the use of template processing classes
3.1 Most basic examples
For simplicity's sake, assume that template files, php files using templates, and template processing classes are all placed in the same directory. The custom in Phplib is to use the ihtml suffix as the suffix of the template file.
The following is the template file to use:

<title> using templates for tests </title>
<body>
The current time is {currenttime}!
</body>

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

Next, use the template processing class to process the template above:

?
Introduction of 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 ();

Load test.ihtml Template
$template->set_file ("Handle1", "test.ihtml");

Replace the currenttime in the template with $timenow values
$template->set_var ("CurrentTime", $timeNow);

Do the actual template operation
$template->parse ("Output", "handle1");

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

Note: If you only want to use the template class in Phplib, simply include the Template.inc class in the file header. When you create a Template object, you can specify a template file path, such as: New Template ("/htdocs/apps/templates/"), and the default is 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 needs to be explained that the purpose of a set block is not nested, but this example contains both. Please read carefully, block set is to avoid this situation: the original can be in a template file (static page) to complete the content, due to the need for part of the loop, but the part of the loop to extract a separate template file. Think, if you don't use a block setting, does this example require 3 template files?
Template file 1,page.ihtml

<body bgcolor= "#ffffff" >
<table border=1 cellpadding=4 cellspacing=0 bgcolor= "#eeeeee" >
< Tr>
<td colspan=2> </tr>
<tr>
< Td>{out}</td>
<td>Content</td>
</tr>
</table>
</body>

Template file 2, box.ihtml

<!--start box.ihtml-->
<table border=1 bgcolor= "#cccccc" cellpadding=4 cellspacing=0>
<tr>
<TD colspan=2><b>{title}</b></td>
</tr>
<!--BEGIN Row-->
<tr>
<td>{NUM}</td>
<td>{bignum}
</tr>
<!--end Row-->
</table>
<!--end Box.ihtml-->

Template processing files,test.php

<?php
//Introduction of template class
Include ("Template.inc");

#实例化一个Template类, it's called $t.
$t = new Template();

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

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

# Set Replacement
$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" => $n, "Bignum" => $nn));
#进行分析, the results of the analysis are added to the back of rows
$t->parse ("Rows", "Row", True);
}

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

# Output Final results
$t->p ("Out");
?>



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.