<? Php
/* @ Author: zhuyubing@gmail.com */
Class Template {
Var $ code;
Function Template ($ template ){
$ This-> code = implode ('', @ file ($ template ));
}
Function assign ($ name, $ var = null ){
If (is_string ($ name) & is_string ($ var )){
$ This-> code = str_replace ('{'. $ name. '}', $ var, $ this-> code );
} Else if (is_array ($ var )){
List ($ this-> code, $ tmp, $ end) = explode ('<! -- '. $ Name.' --> ', $ this-> code );
While (list (, $ v) = each ($ var )){
$ T = $ tmp; $ k2 = $ v2 = '';
While (list ($ k2, $ v2) = each ($ v )){
$ T = str_replace ('{'. $ k2. '}', $ v2, $ t );
}
$ This-> code. = $ t;
}
$ This-> code. = $ end;
} Else {
While (list ($ k2, $ v2) = each ($ name )){
$ This-> code = str_replace ('{'. $ k2. '}', $ v2, $ this-> code );
}
}
}
Function display (){
Echo $ this-> code;
}
}
?>
The simplest hello_world
Compile a PHP template file hello_world.html
<Html>
<Head>
<Meta http-equiv = "Content-Type" c/>
<Title> {title} </title>
</Head>
<Body>
<H1> {title}
</Body>
</Html>
The next step is the template translation.
<? Php
Include ('../include/template. Php'); // contains the template core class file
$ Tpl = new Template('hello_world.html '); // The parameter is the template path and file name. You can use a relative path or an absolute path.
$ Tpl-> assign ('title', "hello world! "); // Replace {title} with hello world
$ Tpl-> display ();
?>
Use arrays in the template
Test_array.html
<Html>
<Body>
<H3> {user} {email}
<H3> {user1} {email1}
</Body>
</Html>
Template processing file
<? Php
Include ('../include/template. Php ');
$ Tpl = new Template('test_array.html ');
$ User = array ('user' => 'ubing', 'Email '=> 'test @ sina.com ');
$ Tpl-> assign ($ user );
$ Tpl-> assign ('user1', 'Jack ');
$ Tpl-> assign ('email1', 'zhuyubing @ gmail.com ');
$ Tpl-> display ();
?>
Simple block processing
Block.html
<Table width = "400" border = "1">
<Tr>
<Td> User Name </td>
<Td> email </td>
</Tr>
<! -- Users -->
<Tr>
<Td> {name} </td>
<Td> {email} </td>
</Tr>
<! -- Users -->
</Table>
Block processing program
Block. php
<? Php
Include ('../include/template. Php ');
$ Tpl = new Template('block.html ');
$ Users = array (
Array ('name' => 'Jack', 'Email '=> 'test @ example.com '),
Array ('name' => 'Tom ', 'Email' => 'Tom @ sina.com ')
);
$ Tpl-> assign ('users', $ users );
$ Tpl-> display ();
?>
Template inclusion test
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" c/>
<Title> {title} </title>
</Head>
<Body>
<H1> {title}
<P> {block} </p>
</Body>
</Html>
Template contains handler
<? Php
Include ('../include/template. Php ');
$ Tpl = new Template('block.html ');
$ Users = array (
Array ('name' => 'Jack', 'Email '=> 'test @ example.com '),
Array ('name' => 'Tom ', 'Email' => 'zhuyubing @ gmail.com '),
);
$ Tpl-> assign ('users', $ users );
$ Block = $ tpl-> code;
$ Tpl-> Template('main.html ');
$ Tpl-> assign ('block', $ block );
$ Tpl-> assign (array ('title' => 'test multi-template file '));
$ Tpl-> display ();
?>