Template Manual
Directory sequence function introduction Using Example 1. The simplest variable replacement 2. Use an array to do variable replacement 2. Use a list (ie, loop display) 2. Use list nested 2. Template criteria judgment output
The prelude is now a very good template engine, like Smarty's exceptionally perfect engine, which does have a place worth going to study, and the so-called template compilation technology in my personal memory seems to be smarty exclusive launch, I don't know Do you want to remember Discuz! This Chinese launched forum, in fact, template compilation technology is used in Discuz! The only shortcomings of Smarty Templates (individual think) is too large, the reason is that he provides a lot of additional features. My small template class, just provides basic functions to parsing and processing template. The template itself is something that provides an interface scalability. Smarty is too complicated. Even when I first used Smarty, I spent a lot. Kung Fu goes with his template mark language. Recently I read the << overload template engine >> published by Brian Lozier, feels non-shallow, reason, 25 lines of code to implement a template engine. So use the work, expand This class. It provides a function similar to compilation and infinite nested. Template class file download: template.inc.rar My mail: stangly_wrong@msn.com My blog: blog.9cbs.net/stangly
Function introduction Template's parsing template's multi-layer nested template's type can be an HTM template tag or a PHP file
The simplest variable replacement
Test_1.tplthis is a {color} apple font>.
Test_1.php
Require_once ("../ Includes / Template / Template.inc.php");
$ TPL = New Template;
$ TPL-> setvar ('color', 'red');
$ TPL-> setCachedir ('./ cache /');
$ TPL-> setfile ('./ TPLS / TEST_1.TPL', TPL_HTM);
Echo $ TPL-> PARSE ();
?>
Output Resultthis is a red apple.
Use arrays to do variable replacement
Test_2.tplthis is a {color} {what} font>.
TEST_2.PHP
Require_once ("../ Includes / Template / Template.inc.php");
$ d = array ();
$ D ['color'] = 'red';
$ D ['what'] = 'apple';
$ TPL = New Template;
$ TPL-> setvar ($ d);
$ TPL-> setCachedir ('./ cache /');
$ TPL-> setfile ('./ TPLS / TEST_2.TPL', TPL_HTM);
Echo $ TPL-> PARSE ();
?>
Output Resultthis is a red apple.
Use list (ie loop display)
Test_3.tplthis is a {color} {what} font>
{list user_list as user} User's account name: {user ['name'] }
User Password: {User ['Pass']}
{endlist} test_3.php
Require_once ("../ Includes / Template / Template.inc.php");
$ d = array ();
$ D ['what'] = 'apple';
$ D ['color'] = 'red';
$ D ['user_list'] = array ();
$ D ['user_list'] [] = array (
'name' => 'Stangly',
'PASS' => '123456',
);
$ D ['user_list'] [] = array (
'name' => 'jear',
'PASS' => '123456',
);
$ TPL = New Template;
$ TPL-> setvar ($ d);
$ TPL-> setCachedir ('./ cache /');
$ TPL-> setfile ('./ TPLS / TEST_3.TPL', TPL_HTM);
Echo $ TPL-> PARSE ();
?>
Output Resultthis Is A Red Apple. User | STANGLY User Password: 123456 User | JEAR User Password: 123456
The list nested this example is based on the list (ie, the loop display) main program, does not change the PHP program, only the template file is simple to modify, and the list nested function is realized. It is actually how your array will organize. Only throwing bricks.
Test_4.tplthis is a {color} {what} font>
{list user_list as user} {list user as key => var} {key }: {var}
{endlist}
{endlist}
Test_4.php
Require_once ("../ Includes / Template / Template.inc.php");
$ d = array ();
$ D ['what'] = 'apple';
$ D ['color'] = 'red';
$ D ['user_list'] = array ();
$ D ['user_list'] [] = array (
'name' => 'Stangly',
'pass' => '123456',);
$ D ['user_list'] [] = array (
'name' => 'jear',
'PASS' => '123456',
);
$ TPL = New Template;
$ TPL-> setvar ($ d);
$ TPL-> setCachedir ('./ cache /');
$ TPL-> setfile ('./ TPLS / TEST_3.TPL', TPL_HTM);
Echo $ TPL-> PARSE ();
?>
Output Resultthis Is A Red Apple. User | STANGLY User Password: 123456 User | JEAR User Password: 123456
Template condition judgment output
TEST_5.TPL {IF A1 == B1} A1 == B1 {endif}
{IF A2! = B2} A2! = b2 {endif}
{IF A3
TEST_5.PHP
Require_once ("../ Includes / Template / Template.inc.php");
$ d = array ();
$ D ['A1'] = 1;
$ D ['b1'] = 1;
$ D ['A2'] = 1;
$ D ['b2'] = 2;
$ D ['A3'] = 1;
$ D ['b3'] = 2;
$ D ['A4'] = 2;
$ D ['B4'] = 1;
$ TPL = New Template;
$ TPL-> setvar ($ d);
$ TPL-> setvar ('a', '1');
$ TPL-> setvar ('b', '1');
$ TPL-> setCachedir ('./ cache /');
$ TPL-> setfile ('./ TPLS / TEST_4.TPL', TPL_HTM);
Echo $ TPL-> PARSE ();
?>
Output resulta1 == B1A2! = B2A3