• 技术文章 >后端开发 >php教程

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

    2016-06-13 12:55:43原创287
    晒一下我的模板引擎,欢迎拍砖
    本帖最后由 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('<', $s);
    $this->data[0] = '';
    $this->find_dsn();
    }
    //新增 run 方法,
    function run() {
    //include "data://," . join('<', $this->data);
    eval('?>' . join('<', $this->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[1] && $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] = "<$code_start" . $t[1];
    $this->data[$start] = join('>', $t);
    $this->data[$end] = "$code_end<" . $this->data[$end];
    break;
    case 1:
    $this->data[$end] .= "<$code_end";
    $this->data[$start] = "$code_start<" . $this->data[$start];
    break;
    default:
    $n = round(100/$m[1]);
    $this->data[$end] .= "<$code_end";
    $this->data[$start] = "$code_start
    <" . $this->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<=$end; $i++) {
    $code .= '<' . $this->data[$i];
    if($i > $start) unset($this->data[$i]);
    }
    $code = addslashes($code);
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:nbsp this data gt start
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• 20+值得了解的PHP面试题(附答案分析)• 庆祝新版第二贴解决方法 • 找到一个编辑器,但是不知道来得到里面的值!求解解决方案 • 问大家一个gbk和gb2312的有关问题 • webservice有关问题,soap有关问题,响应有关问题-求大神!大神在哪
    1/1

    PHP中文网