-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.php
More file actions
69 lines (61 loc) · 1.87 KB
/
template.php
File metadata and controls
69 lines (61 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* 简单的PHP模板渲染类
*
* @author standsun@126.com
* @date 2017-11-07
*
*/
class Template {
// 编译文件存放地址
const RUNTIME_PATH = '/tmp/';
// 模板变量
private $_vars = array();
// 渲染模板并返回渲染后的code
public function render($templateFile) {
if(!file_exists($templateFile)) {
throw new \Exception("模板文件不存在");
}
$runtimeFile = self::RUNTIME_PATH . 'cache.' . md5($templateFile) . '.' . date('YmdHi') . '.txt';
if(!file_exists($runtimeFile)) {
// 获取初始内容转换标签为php标签
$content = file_get_contents($templateFile);
$content = $this->replace($content);
// 保存编译后的文件
file_put_contents($runtimeFile,$content);
}
// 生成模板变量
extract($this->_vars);
// 引入模板转换后的文件
return include $runtimeFile;
}
// 模板中的变量赋值
public function assign($key,$value = null) {
if(is_array($key)) {
foreach($key as $k=>$v) {
$this->_vars[$k] = $v;
}
}elseif(is_string($key)) {
$this->_vars[$key] = $value;
}
return $this;
}
/*
* 替换模板中的标签
*/
private function replace($string) {
$pattern = array(
'#\{\$(\w+)\}#',
'#\{\$(\w+)\.(\w+)\}#',
'#\{foreach\s+(\w+)\s+(\w+)=>(\w+)\}#',
'#\{/foreach\}#'
);
$replace = array(
'<?php echo isset($\1)?$\1:""; ?>',
'<?php echo isset($\1["\2"])?$\1["\2"]:""; ?>',
'<?php if(!isset($\1)) $\1=array();foreach($\1 as $\2=>$\3): ?>',
'<?php endforeach; ?>'
);
return preg_replace($pattern,$replace,$string);
}
}