简单php教程模板引擎的实现例子
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
</head><body>
{$var}
<br />{loop $arr $v}
{$v} |
{/loop}
</body>
</html>
index.php调用文件
<?php
header("content-type:text/html; charset=utf-8");
require_once "./include/common.inc.php";$var = 'abc';
$arr = array(1, 2, 3);include template('index');
?>
/common.inc.php文件
<?php
define('in_site', true);
define('site_root', substr(dirname(__file__), 0, -7));require_once site_root.'./include/template.func.php';
$tplrefresh = 1; //设置是否检查更新
$tpldir = site_root.'./templates/default/'; //模板存放目录
$objdir = site_root.'./sitedata/templates/'; //模板编译文件存放目录
?>
template.func.php文件
<?
if(!defined('in_site')) {
exit('access denied!');
}function checktplrefresh($tplfile, $timecompare) {
global $tplrefresh;if($tplrefresh == 1 && @filemtime($tplfile) != $timecompare) {
parse_template($tplfile);
}
return true;
}function template($file) {
global $tpldir, $objdir;$objfile = $objdir.$file.'.tpl.php';
if(!file_exists($objfile)) {
$tplfile = $tpldir.$file.'.htm';
parse_template($tplfile);
}
return $objfile;
}function parse_template($tplfile) {
global $tpldir, $objdir;$file = basename($tplfile, '.htm');
$objfile = $objdir.$file.'.tpl.php';if(!$fp = @fopen($tplfile, 'r')) {
exit("current template file '$tplfile' not found or have no access!");
}
$template = fread($fp, max(filesize($tplfile), 1));
fclose($fp);$template = preg_replace("/[nrt]*{subtemplates+([a-z0-9_:]+)}[nrt]*/is", "<?php include template('1'); ?>", $template);
$template = preg_replace("/([nr]+)t+/s", "1", $template);
$template = preg_replace("/<!--{(.+?)}-->/s", "{1}", $template);
$template = preg_replace("/{($[a-za-z0-9_[]'"$.x7f-xff]+)}/s", "<?php echo 1; ?>", $template);$template = preg_replace("/[nrt]*{evals+(.+?)}[nrt]*/is", "<?php 1; ?>", $template);
$template = preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/is", "<?php echo 1; ?>", $template);
$template = preg_replace("/([nrt]*){elseifs+(.+?)}([nrt]*)/is", "1<?php } elseif(2) { ?>3", $template);
$template = preg_replace("/([nrt]*){else}([nrt]*)/is", "1<?php } else { ?>2", $template);for($i = 0; $i < 5; $i++) {
$template = preg_replace("/[nrt]*{loops+(s+)s+(s+)}[nr]*(.+?)[nr]*{/loop}[nrt]*/is", "<?php if(is_array(1)) { foreach(1 as 2) { ?>3<?php } } ?>", $template);
$template = preg_replace("/[nrt]*{loops+(s+)s+(s+)s+(s+)}[nrt]*(.+?)[nrt]*{/loop}[nrt]*/is", "<?php if(is_array(1)) { foreach(1 as 2 => 3) { ?>4<?php } } ?>", $template);
$template = preg_replace("/([nrt]*){ifs+(.+?)}([nr]*)(.+?)([nr]*){/if}([nrt]*)/is", "1<?php if(2) { ?>345<?php } ?>6", $template);
}$template = preg_replace("/{([a-za-z_x7f-xff][a-za-z0-9_x7f-xff]*)}/s", "<?php echo 1; ?>", $template);
$template = preg_replace("/ ?>[nr]*<?php /s", " ", $template);$template = preg_replace("/"(http)?[w./:]+?[^"]+?&[^"]+?"/e", "transamp('')", $template);
$template = preg_replace("/<script[^>]*?src="(.+?)"(.*?)>s*</script>/ise", "strips教程criptamp('1', '2')", $template);$template = "<?php if(!defined('in_site')) exit('access denied!'); checktplrefresh('$tplfile', ".time()."); ?>n$template";
if(!$fp = @fopen($objfile, 'wb')) {
exit("directory '$objdir' not found or have no access!");
}
flock($fp, lock_ex);
fwrite($fp, $template);
flock($fp, lock_un);
fclose($fp);
}function transamp($str) {
$str = str_replace('&', '&', $str);
$str = str_replace('&amp;', '&', $str);
$str = str_replace('"', '"', $str);
return $str;
}function stripscriptamp($s, $extra) {
$extra = str_replace('"', '"', $extra);
$s = str_replace('&', '&', $s);
return "<script src="$s" type="text/网页特效"$extra></script>";
}
?>