Php template writing

Source: Internet
Author: User

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

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.