class CompileClass {
private $template; // 待编译文件
private $content; // 需要替换的文本
private $compile_file; // 编译后的文件
private $left = '{'; // 左定界符
private $right = '}'; // 右定界符
private $include_file = array(); // 引入的文件
private $config; // 模板的配置文件
private $T_P = array(); // 需要替换的表达式
private $T_R = array(); // 替换后的字符串
public function __construct($template, $compile_file, $config) {}
public function compile() {
$this->c_include();
$this->c_var();
$this->c_staticFile();
file_put_contents($this->compile_file, $this->content);
}
// 处理include
public function c_include() {}
// 处理各种赋值和基本语句
public function c_var() {}
// 对静态的JavaScript进行解析
public function c_staticFile() {}
}
// 需要替换的正则表达式
$this->T_P[] = "/$this->left\s*\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\xf7-\xff]*)\s*$this->right/";
$this->T_P[] = "/$this->left\s*(loop|foreach)\s*\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\xf7-\xff]*)\s*$this->right/";
$this->T_P[] = "/$this->left\s*(loop|foreach)\s*\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\xf7-\xff]*)\s+"
. "as\s+\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\xf7-\xff]*)$this->right/";
$this->T_P[] = "/$this->left\s*\/(loop|foreach|if)\s*$this->right/";
$this->T_P[] = "/$this->left\s*if(.*?)\s*$this->right/";
$this->T_P[] = "/$this->left\s*(else if|elseif)(.*?)\s*$this->right/";
$this->T_P[] = "/$this->left\s*else\s*$this->right/";
$this->T_P[] = "/$this->left\s*([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\xf7-\xff]*)\s*$this->right/";
// 替换后的字符串
$this->T_R[] = "<?php echo \$\\1; ?>";
$this->T_R[] = "<?php foreach((array)\$\\2 as \$K=>\$V) { ?>";
$this->T_R[] = "<?php foreach((array)\$\\2 as &\$\\3) { ?>";
$this->T_R[] = "<?php } ?>";
$this->T_R[] = "<?php if(\\1) { ?>";
$this->T_R[] = "<?php } elseif(\\2) { ?>";
$this->T_R[] = "<?php } else { ?>";
$this->T_R[] = "<?php echo \$\\1; ?>";
<!--模板文件-->
{$data}
{foreach $vars}
{if $V == 1 }
<input value="{V}">
{elseif $V == 2}
<input value="123123">
{else }
<input value="sdfsas是aa">
{/if}
{/foreach}
{ loop $vars as $var}
<input value="{var}">
{ /loop }
// 解析后
<?php echo $data; ?>
<?php foreach((array)$vars as $K=>$V) { ?>
<?php if( $V == 1) { ?>
<input value="<?php echo $V; ?>">
<?php } elseif( $V == 2) { ?>
<input value="123123">
<?php } else { ?>
<input value="sdfsas是aa">
<?php } ?>
<?php } ?>
<?php foreach((array)$vars as &$var) { ?>
<input value="<?php echo $var; ?>">
<?php } ?>
class Template {
// 配置数组
private $_arrayConfig = array(
'root' => '', // 文件根目录
'suffix' => '.html', // 模板文件后缀
'template_dir' => 'templates', // 模板所在文件夹
'compile_dir' => 'templates_c', // 编译后存放的文件夹
'cache_dir' => 'cache', // 静态html存放地址
'cache_htm' => false, // 是否编译为静态html文件
'suffix_cache' => '.htm', // 设置编译文件的后缀
'cache_time' => 7200, // 自动更新间隔
'php_turn' => true, // 是否支持原生php代码
'debug' => 'false',
);
private $_value = array();
private $_compileTool; // 编译器
static private $_instance = null;
public $file; // 模板文件名
public $debug = array(); // 调试信息
public function __construct($array_config=array()) {}
// 单步设置配置文件
public function setConfig($key, $value=null) {}
// 注入单个变量
public function assign($key, $value) {}
// 注入数组变量
public function assignArray($array) {}
// 是否开启缓存
public function needCache() {}
// 如果需要重新编译文件
public function reCache() {}
// 显示模板
public function show($file) {}
}
public function show($file) {
$this->file = $file;
if(!is_file($this->path())) {
exit("找不到对应的模板文件");
}
$compile_file = $this->_arrayConfig['compile_dir']. md5($file). '.php';
$cache_file = $this->_arrayConfig['cache_dir']. md5($file). $this->_arrayConfig['suffix_cache'];
// 如果需要重新编译文件
if($this->reCache($file) === false) {
$this->_compileTool = new CompileClass($this->path(), $compile_file, $this->_arrayConfig);
if($this->needCache()) {
// 输出到缓冲区
ob_start();
}
// 将赋值的变量导入当前符号表
extract($this->_value, EXTR_OVERWRITE);
if(!is_file($compile_file) or filemtime($compile_file) < filemtime($this->path())) {
$this->_compileTool->vars = $this->_value;
$this->_compileTool->compile();
include($compile_file);
}
else {
include($compile_file);
}
// 如果需要编译成静态文件
if($this->needCache() === true) {
$message = ob_get_contents();
file_put_contents($cache_file, $message);
}
}
else {
readfile($cache_file);
}
}
require('Template.php');
$config = array(
'debug' => true,
'cache_htm' => false,
'debug' => true
);
$tpl = new Template($config);
$tpl->assign('data', microtime(true));
$tpl->assign('vars', array(1,2,3));
$tpl->assign('title', "hhhh");
$tpl->show('test');
<!DOCTYPE html>
<html>
<head>
<title>hhhh</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
1466525760.32 <input value="1">
<input value="123123">
<input value="sdfsas是aa">
<input value="1">
<input value="2">
<input value="3">
<script src="123?t=1465898652"></script>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有