一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

简单PHP模板引擎的实现例子

时间:2022-06-25 01:00:52 编辑:袖梨 来源:一聚教程网

简单php教程模板引擎的实现例子






{$var}

{loop $arr $v}
{$v} |
{/loop}

index.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文件

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", "", $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", "", $template);

    $template = preg_replace("/[nrt]*{evals+(.+?)}[nrt]*/is", "", $template);
    $template = preg_replace("/[nrt]*{echos+(.+?)}[nrt]*/is", "", $template);
 $template = preg_replace("/([nrt]*){elseifs+(.+?)}([nrt]*)/is", "13", $template);
 $template = preg_replace("/([nrt]*){else}([nrt]*)/is", "12", $template);

 for($i = 0; $i < 5; $i++) {
        $template = preg_replace("/[nrt]*{loops+(s+)s+(s+)}[nr]*(.+?)[nr]*{/loop}[nrt]*/is", "3", $template);
        $template = preg_replace("/[nrt]*{loops+(s+)s+(s+)s+(s+)}[nrt]*(.+?)[nrt]*{/loop}[nrt]*/is", " 3) { ?>4", $template);
  $template = preg_replace("/([nrt]*){ifs+(.+?)}([nr]*)(.+?)([nr]*){/if}([nrt]*)/is", "13456", $template);
 }

    $template = preg_replace("/{([a-za-z_x7f-xff][a-za-z0-9_x7f-xff]*)}/s", "", $template);
 $template = preg_replace("/ ?>[nr]*

 $template = preg_replace("/"(http)?[w./:]+?[^"]+?&[^"]+?"/e", "transamp('')", $template);
 $template = preg_replace("/]*?src="(.+?)"(.*?)>s*/ise", "strips教程criptamp('1', '2')", $template);

    $template = "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 "";
}
?>

热门栏目