Tutorials to .com

Tutorials to .com » Php » Others » Template used to deal with the phplib7.2 multiple nested BLOCK

Template used to deal with the phplib7.2 multiple nested BLOCK

Print View , by: iSee ,Total views: 7 ,Word Count: 826 ,Date: Mon, 20 Apr 2009 Time: 2:31 AM

At present, most of the template provided in the block-type functions, block number to deal with the HTML elements of uncertainty, when the emergence of multi-storey element is not set, the nested block will be used, for the use of nested block, after more than test or there is a need to pay attention to, the following note with an example of a nested block approach.

1, we first look at the example of the effect to be achieved (Figure 1):



2, template file test.htm
Template file is a static page, you can use your favorite web page editor of the appearance of the preparation of his


<! DOCTYPE HTML PUBLIC "- / / W3C / / DTD HTML 4.01 Transitional / / EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></ title>
</ head>
<body>
<table width="400" border="1" bordercolor="#000000">
<tr> <td> <div align="center"> (myname) test block </ div> </ td> </ tr>
<tr> <td> animal and plant my garden: </ td> </ tr>
<! - BEGIN animalList ->
<tr> <td> (animal) </ td> </ tr>
<! - BEGIN plantList ->
<tr> <td> (plant) </ td> </ tr>
<! - END plantList ->
<! - END animalList ->
</ table>
</ body>
</ html>

3, the following code is the php file test.php


<? php
include_once ( "template.inc");
$ t = new template (".");
$ myname = "";

/***************************************
array as the first animal in the cycle of block layer data,
plant an array as a second layer of data block in the cycle,
Can also be according to their own needs, to read from the database
Data record
***************************************/
$ animal = array ( "animal", "plant");
$ plant = array (array ( "pig", "white", "Xiao Qiang"), array ( "Rose," "Sunflower"));

$ t-> set_file ( "myfilehandle", "test.htm");

/ / (Myname) template has been to replace the value of $ myname
$ t-> set_var ( "myname", $ myname);

/*************************************
Setting block, first set the first layer of block, file sentence
Handle is myfilehandle, then set the second level block,
File handle is the first layer block. Attention to two statements
Order
*************************************/
$ t-> set_block ( "myfilehandle", "animalList", "a");
$ t-> set_block ( "animalList", "plantList", "p");

/ / Block the beginning of the first layer
for ($ i = 0; $ i <count ($ animal); $ i + +) (

/*******************************
This sentence is very important, not less, for its
Used to remove the last block of the contents of the second layer
Otherwise, if the results (Figure b) below, will be re -
Complex showed that the last block of the contents of the second layer.
The reason is because the parse ( "p", "plantList", true)
Ture of options
******************************/
$ t-> set_var ( "p");

/ / Template (animal) be $ animal [$ i] the value of the replacement
$ t-> set_var ( "animal", $ animal [$ i]);

/ / Block the beginning of the second layer
for ($ j = 0; $ j <count ($ plant [$ i]); $ j + +) (

/ / Template (plant) be $ plant [$ i] [$ j] the value of the replacement
$ t-> set_var ( "plant", $ plant [$ i] [$ j]);

/ / Analysis of the first layer block
$ t-> parse ( "p", "plantList", true);
)

/ / Analysis of second-tier block
$ t-> parse ( "a", "animalList", true);
)
$ t-> parse ( "out", "myfilehandle");
$ t-> p ( "out");
?>



4, the lack of $ set_var ( "p") statement of the results:


Relatively simple example of the need to pay attention to the procedure I have explained the appropriate location. This is only one way to deal with nested block, and we share, we would like to help. Phpx and phpe.net like to thank all our friends to help on the completion of this article.



php other Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.