教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 简单的模板函数

简单的模板函数

发布时间:2023-05-11   编辑:jiaochengji.com
教程集为您提供简单的模板函数等资源,欢迎您收藏本站,我们将为您提供最新的简单的模板函数资源

简单的模板函数<?php
define('DEFAULT_TEMPLATE', 'templates');
define('DEFAULT_TEMPLATE_C', 'templates_c');
define('TEMPLATE_DEPLOY', true);

function template($FileName, $TemplateDir='', $ComplieDir='', $op = ''){
$str = '';
$TemplateDir = empty($TemplateDir) ? DEFAULT_TEMPLATE : $TemplateDir;
$ComplieDir = empty($ComplieDir) ? DEFAULT_TEMPLATE_C : $ComplieDir;
if(TEMPLATE_DEPLOY === false) {
$comtime = @filemtime($ComplieDir . "/" . md5($FileName).".php");
$temtime = @filemtime($TemplateDir . "/" . $FileName);
$str = file_get_contents($TemplateDir . "/" . $FileName);
$checktemplate = preg_match('|{template (.*)}|isU', $str);
}
if(($temtime > $comtime && TEMPLATE_DEPLOY === false) || $checktemplate){
if(empty($str)) {
$str = file_get_contents($TemplateDir . "/" . $FileName);
}
$str = preg_replace('|<!--{if (. )}-->|isU', '<?php if(\\1){ ?>', $str);
$str = preg_replace('|<!--{/if}-->|isU', '<?php } ?>', $str);
$str = preg_replace('|{_(.*)}|isU', '<?php echo \\1; ?>', $str);
$str = preg_replace('|{\$lang_(.*)}|isU', '<?php echo $language["\\1"]; ?>', $str);
$str = preg_replace('|{\$(.*)}|isU', '<?php echo $\\1; ?>', $str);
$str = preg_replace('|<!--{else}-->|isU', '<?php }else{ ?>', $str);
$str = preg_replace('|<!--{elseif (.*)}-->|isU', '<?php }elseif(\\1){ ?>', $str);
$str = preg_replace('|{eval (.*)}|isU', '<?php \\1; ?>', $str);
$str = preg_replace('|{echo (.*)}|isU', '<?php echo \\1; ?>', $str);
if(preg_match_all('|{include_php (.*)}|isU', $str, $phparray)) {
$phpfilename = $phparray[1][0] . ".php";
if(is_file($phpfilename)) {
$phpcontent = str_replace("<?php", "<?", file_get_contents($phpfilename));
$phpcontent = str_replace("?>", " ?>", $phpcontent);
$phpcontent = str_replace("<?", "<?php ", $phpcontent);
$str = $str . $phpcontent;
}
$str = preg_replace('|{include_php (.*)}|isU', '', $str);
}

$str = preg_replace('|{include_htm(.*) (.*)}|isU', '<?php if(is_file("' . $TemplateDir . '/\\2.htm\\1")){ $__Dstr = file_get_contents("' . $TemplateDir . '/\\2.htm\\1"); echo $__Dstr;} ?>', $str);
$str = preg_replace('|<!--{loop (.*) (.*) (.*)}-->|isU', '<?php if(is_array(\\1)){ foreach(\\1 as \\2 => \\3){ ?>', $str);
$str = preg_replace('|<!--{for (.*)}-->|isU', '<?php for (\\1) { ?>', $str);
$str = preg_replace('|<!--{/for}-->|isU', '<?php } ?>', $str);
$str = preg_replace('|<!--{/loop}-->|isU', '<?php }} ?>', $str);
if(preg_match_all('|{template (.*)}|isU', $str, $templatearray)) {
$str = preg_replace('|{template (.*)}|isU', '', $str);
$templatefilename = $templatearray[1][0];
if(is_file($TemplateDir . '/' .$templatefilename)) {
$templatecontent = template($templatefilename, $TemplateDir, $ComplieDir, 1);
$str = $str . $templatecontent;
}
}
$str = preg_replace('|\t|isU', '', $str);
$str = preg_replace('|\r\n|isU', '', $str);
$str = preg_replace('|\n|isU', '', $str);
$str = preg_replace('|(\?\>\<\?php)|isU', '', $str);
if(!$op) {
@file_put_contents($ComplieDir . "/" . md5($FileName) . '.php', $str);
}
}
if($op) {
return $str;
}
return $ComplieDir . "/" . md5($FileName) . '.php';
}

您可能感兴趣的文章:
php模版生成html的小例子
Golang 模板(text/template) (一)
golang 模板(template)的常用基本语法
PHP模板引擎smarty生成随机数 smarty中math函数用法
django模板引擎是什么
django模板引擎是什么意思
优化PHP程序
php模板
flask模板是什么
php模板引擎有哪些

[关闭]
~ ~