欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

晒一上小弟我的模板引擎,欢迎拍砖

程序员文章站 2024-02-08 20:23:10
...
晒一下我的模板引擎,欢迎拍砖
本帖最后由 xuzuning 于 2012-05-23 15:02:49 编辑 别不多说,直接上代码
template.php(3.55K)

class template {
protected $data = array();
protected $drillmode = 0;
function __construct($s) {
if(file_exists($s)) $s = file_get_contents($s);
$this->find_var($s);
$this->data = explode(' $this->data[0] = '';
$this->find_dsn();
}
//新增 run 方法,
function run() {
//include "data://," . join('data);
eval('?>' . join('data));
}
function find($pattern) {
$this->pattern = $pattern;
return array_filter($this->data, array($this, 'find_callback'));
}
private function find_dsn() {
foreach($this->find("#\bdsn\b#i") as $k=>$v) {
$t = $this->find_tag($tag = strtok($v, ' '), $k);
end($t);
$dsn[] = array( $k, key($t) );
}
if($this->drillmode) {
foreach($this->find("#\bdrill\b#i") as $k=>$t) {
foreach($dsn as $i=>$v) if($k $v[0]) $t = $i;
$drill[] = $dsn[$t];
unset($dsn[$t]);
}
}
foreach($dsn as $v) {
list($start, $end) = $v;
preg_match('/\bdsn\s*=\s*([^\s>]+)/i', $this->data[$start], $reg);
$this->data[$start] = str_replace(' '.$reg[0], '', $this->data[$start]);

$m = explode(',', trim($reg[1], '\'"')) + array(0, 0, '');
$code_start = "?php if(isset(\$_var))\$_st[]=\$_var;foreach((isset(\$_var['$m[0]'])?\$_var['$m[0]']:\$this->$m[0]('$m[1]','$m[2]')) as \$_key=>\$_var){?>";
$code_end = "?php }\$_var=array_pop(\$_st);?>";
switch($m[1]) {
case 0:
$t = explode('>', $this->data[$start]);
$t[1] = " $this->data[$start] = join('>', $t);
$this->data[$end] = "$code_enddata[$end];
break;
case 1:
$this->data[$end] .= " $this->data[$start] = "$code_startdata[$start];
break;
default:
$n = round(100/$m[1]);
$this->data[$end] .= " $this->data[$start] = "$code_start
data[$start];
break;
}
}
if($this->drillmode) foreach($drill as $v) {
list($start, $end) = $v;
preg_match('/\bdsn\s*=\s*([^\s>]+)/i', $this->data[$start], $reg);
$this->data[$start] = str_replace(' '.$reg[0], '', $this->data[$start]);
$m = explode(',', trim($reg[1], '\'"')) + array(0, 0, '');
$code = '';
for($i=$start; $i $code .= 'data[$i];
if($i > $start) unset($this->data[$i]);
}
$code = addslashes($code);
晒一上小弟我的模板引擎,欢迎拍砖

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 晒一上小弟我的模板引擎,欢迎拍砖
  • 专题推荐