<?
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 result
This is a red apple .
使用数组去做变量替换
test_2.tpl
This is a {color} <font color="{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();
?>
<?
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();
?>
<?
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();
?>
<?
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();
?>
本文地址:http://com.8s8s.com/it/it24614.htm