Tutorials to .com

Tutorials to .com » Php » Others » PHP-Web Application Development: Using templates

PHP-Web Application Development: Using templates

Print View , by: iSee ,Total views: 18 ,Word Count: 1720 ,Date: Mon, 20 Apr 2009 Time: 3:38 AM

Each carried out a large-PHP-Web application Programming developers probably have the following experience: spend a lot of time to write hypertext language for page layout, such as double as art; or in the integration and the HTML code in the static page when a lot of time spent. Indeed, the use of scripting languages is not easy to develop Web applications to data processing and data show that separate, but co-operation in many cases, if the data and display can not be separated, it would greatly affect the development of efficient, professional division of the play. To solve this problem, php also offers its own solution, a combination of factors, this article introduces the Template category PHPLIB.

1 template design class
The main categories of templates to deal with the tasks required to complete the following:
Document from a template used to read the HTML code.
Template file will be generated and the actual data with the results of the output generated.
At the same time allowing a number of templates to deal with.
To allow the nested template.
To allow a template to deal with separate parts.

Summarized in the above-mentioned tasks, templates for the kind of design goals: a document from a number of templates in the time of the HTML code to display these show the need for dynamic data code of the local operator is replaced by the PHP process the data, and then according to the order of output . Among them, the replacement can be set free.

Be used to read the HTML code used Reading papers
Template file and data is then combined with the use of replacement
To deal with a number of templates used to achieve the storage array.
Nested template to achieve the main idea is: to the template and output (any intermediate results of the analysis) the same can be used as replacement, can be realized.
Dealt with a separate part of the template file in the label set, and then replace in the combination of regular marking to control, to achieve the replacement part.

2 the realization of the template class
PHPLib see the Template.inc, a total of 345 lines of code, there are detailed notes. The following are examples of some of the major function of reference for study:
1) function set_file ($ handle, $ filename = "") line 77, read the document
2) function set_var ($ varname, $ value = "") line 119, set the map data - the replacement of variables
3) function set_block ($ parent, $ handle, $ name = "") line 96, set the mark
4) function subst ($ handle) line 136, replace the implementation of data
5) function parse ($ target, $ handle, $ append = false) line 165, the implementation of the template file and data binding
6) function p ($ varname) line 268, output the results
Note: I downloaded the php-lib7.2c document Template.inc the first 95 lines of less "/", together with normal use.

3 the use of class template
3.1 Examples of the most basic
The sake of simplicity, here the assumption that template file, use the PHP template files and template class files are placed on the same directory. PHPLIB in the habit of using ihtml suffix suffix for template files.
The following is the template file to use:

<title> test using a template </ title>
</ head>
<h2> This is a test document using a template! </ h2>
The current time is (currenttime)!
</ body>
</ html>
Note: The template file and the HTML document is usually more or less the same only difference is the use of "()" enclose a template that can be replaced to deal with dynamic content-type variables.

Then use the template class to deal with the above template:
/ / Introduction of Template Class
include ( "template.inc");

/ / Get the data needs to be replaced
$ timeNow = date ( "Ymd H: i: s", time ());

/ / Instantiate a Template Class
$ template = new Template ();

/ / Load template test.ihtml
$ template-> set_file ( "handle1", "test.ihtml");

/ / Use the replacement value of $ timeNow template currenttime
$ template-> set_var ( "currenttime", $ timeNow);

/ / The template for the actual operation
$ template-> parse ( "output", "handle1");

/ / Output the final results
$ template-> p ( "output");

Note: If you want to use the template PHPLIB category contains only the first in the document can Template.inc category.
Create a Template object, you can specify the template file path, such as: new Template ( "/ htdocs / apps / templates /"), the default for the current path.

3.2 templates and block nested set
The following example from the reference manual PHPLIB, more comprehensive, to be described here is the purpose of setting block has nothing to do with nesting, but this example includes both. Please read the block set in order to avoid such a situation: they could have a template file (static pages) to complete the content, in part because of the need for recycling, and extract the contents of some of the cycle into a separate template file. Please think, do not block if set, this example is a template rather than the needs of three documents?
Template file 1, page.ihtml
<head> <title> (PAGETITLE) </ title> </ head>
<body bgcolor="#ffffff">
<table border=1 cellpadding=4 cellspacing=0 bgcolor="#eeeeee">
<td colspan=2> <h1> (PAGETITLE) </ h1> </ td>
</ tr>
<td> (OUT) </ td>
<td> Content </ td>
</ tr>
</ table>
</ body>
</ html>

Template file 2, box.ihtml
<! - Start box.ihtml ->
<table border=1 bgcolor="#cccccc" cellpadding=4 cellspacing=0>
<td colspan=2> <b> (TITLE) </ b> </ td>
</ tr>
<! - BEGIN row ->
<td> (NUM) </ td>
<td> (BIGNUM)
</ tr>
<! - END row ->
</ table>
<! - End box.ihtml ->

Template file, test.php
<? php
/ / Introduction of Template Class
include ( "template.inc");

# Instantiate a Template class, name is $ t
$ t = new Template ();

# Set up the array contains the template file
$ t-> set_file (array (
"page" => "page.ihtml",
"box" => "box.ihtml"));

# Load template file box in a block row, invoke the name of rows
$ t-> set_block ( "box", "row", "rows");

# Set up the replacement of
$ t-> set_var (array ( "TITLE" => "Testpage",
"PAGETITLE" => "hugo"));

# Generate data on NUM, BIGNUM
for ($ i = 1; $ i <= 3; $ i + +) (
$ n = $ i;
$ nn = $ i * 10;
# Set up the replacement of
$ t-> set_var (array ( "NUM" => $ n, "BIGNUM" => $ nn));
# Under analysis and the results added to the back rows
$ t-> parse ( "rows", "row", true);

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

# Output the final results
$ t-> p ( "OUT");

Note: This document template page.ihtml named variables with the final output handle are the "OUT".
Value part of the cycle type of database, data can be generated with the combination of database application.
The results of the implementation plan are as follows:

Template block nested set with the results of the implementation plan

4 Summary
This paper briefly introduced in PHPLIB Class Template design, implementation and use. Of course, there are many other PHP template programs, such as Perl evolved from the FastTemplates. Internet group is currently using another program, the realization of the main template file is put in storage, the use of eval to achieve data and the combination of template files, such as, relatively speaking template file templates to deal with the management and the use of relatively simple, but the lack of model files. I am currently doing some attempt to combine the two and would like to PHPLIB the Template based on the type of improvement. Initial objectives and tasks are two: 1, in the template file to read when the database to extend its support, so that, when necessary, enhance the flexibility and the use of database management tools 2, Reduction of the template file and data binding, because in actual use in most cases without the variable template files in handling the process and then set up (data variable mapping) again.
In the hope that readers, PHP procedures involved with lovers, with more valuable comments, Good luck!

php other Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.