1. [代码][PHP]代码
', $text);
$text = str_replace('{/loop}', '', $text);
$text = str_replace('{foreachelse}', '', $text);
$text = str_replace('{/foreach}', '', $text);
$text = str_replace('{else}', '', $text);
$text = str_replace('{loopelse}', '', $text);
// template pattern tags //
$pattern = array(
'/\$(\w*[a-zA-Z0-9_])/',
'/\$this\-\>vars\[\'(\w*[a-zA-Z0-9_])\'\]+\.(\w*[a-zA-Z0-9])/',
'/\{include file=(\"|\'|)(\w*[a-zA-Z0-9_\.][a-zA-Z]\w*)(\"|\'|)\}/',
'/\{\$this\-\>vars(\[\'(\w*[a-zA-Z0-9_])\'\])(\[\'(\w*[a-zA-Z0-9_])\'\])?\}/',
'/\{if (.*?)\}/',
'/\{elseif (.*?)\}/',
'/\{loop \$(.*) as (\w*[a-zA-Z0-9_])\}/',
'/\{foreach \$(.*) (\w*[a-zA-Z0-9_])\=\>(\w*[a-zA-Z0-9_])\}/'
);
// replacement PHP tags //
$replacement = array(
'$this->vars[\'\1\']',
'$this->vars[\'\1\'][\'\2\']',
'display(\'\2\')?>',
'vars\1\3?>',
'',
'',
'vars[\'\2\']) {?>',
'vars[\'\2\']=>$this->vars[\'\3\']) {?>'
);
// repalce template tags to PHP tags //
$text = preg_replace($pattern, $replacement, $text);
// create compile file //
$compliefile = $this->compiledir . basename($tpl) . '.php';
if ($fp = @fopen($compliefile, 'w')) {
fputs($fp, $text);
fclose($fp);
}
}
/*
* assigns values to template variables
* @param array|string $k the template variable name(s)
* @param mixed $v the value to assign
*/
function assign($k, $v = null)
{
$this->vars[$k] = $v;
}
/*
* ste directory where templates are located
* @param string $str (path)
*/
function templateDir($path)
{
$this->templatedir = $this->pathCheck($path);
}
/*
* set where compiled templates are located
* @param string $str (path)
*/
function compileDir($path)
{
$this->compiledir = $this->pathCheck($path);
}
/*
* check the path last character
* @param string $str (path)
* @return string
*/
function pathCheck($str)
{
return (preg_match('/\/$/', $str)) ? $str : $str . '/';
}
/*
* executes & displays the template results
* @param string $tpl (template file)
*/
function display($tpl)
{
$tplfile = $this->templatedir . $tpl;
if (!file_exists($tplfile)) {
exit('can not load template file : ' . $tplfile);
}
$compliefile = $this->compiledir . $tpl . '.php';
if (!file_exists($compliefile) || filemtime($tplfile) > filemtime($compliefile)) {
$this->parse($tplfile);
}
include_once($compliefile);
}
}
?>
2. [代码][PHP]代码
用 php + mysql 驱动的在线商城系统,我们的目标为中国的中小企业及个人提供最简洁,最安全,最高效的在线商城解决方案,使用了自建的会员积分折扣功能,不同的会员组有不同的折扣,让您的商店吸引更多的后续客户。 系统自动加分处理功能,自动处理会员等级,免去人工处理的工作量,让您的商店运作起来更方便省事 采用了自建的直接模板技术,免去了模板解析时间,提高了代码利用效率 独立开发的购物车系统,使用最
立即学习“PHP免费学习笔记(深入)”;
assign('title','标题');
$tpl->assign('Name','名字');
$contact = array('1'=>'张一','2'=>'zhang2');
$tpl->assign('contact',$contact);
$tpl->display('test.html');
?>
3. [代码][PHP]代码
立即学习“PHP免费学习笔记(深入)”;
无标题文档 {$title} - {$Name}
{foreach $contact key=>val} {$key}: {$val}
{/foreach}
4. [文件] test.zip










