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

模板引擎?No 正则而已

程序员文章站 2022-06-02 23:09:02
...

之前就觉得TP的那个模板引擎太不爽了,非常的蛋疼,所以参照PHPCMS的改了下,哈哈,自己看吧 无 /** * 模板函数 * @param string $template 模板文件名 * @param string $path 模板路径 * @param string $suffix 模板后缀 */function template($template = '', $p

之前就觉得TP 的那个模板引擎太不爽了,非常的蛋疼,所以参照PHPCMS的 改了下, 哈哈,自己看吧
/**
 * 模板函数
 * @param string $template 模板文件名
 * @param string $path  模板路径
 * @param string $suffix 模板后缀
 */
function template($template = '', $path = '', $suffix = '', $show_error = true) {
    $tpl_path = $path ? $path : (config('tpl_path') ? config('tpl_path') : './template/');
    $tpl_suffix = $suffix ? $suffix : (config('tpl_suffix') ? config('tpl_suffix') : '.html');
    if (empty($template)) {
        $template_file = $tpl_path . __MODULE__ . '/' . __CONTROLLER__ . '/' . __ACTION__;
    } else {
        if (!$path) {
            $pcount = substr_count($template, '/');
            if ($pcount == 0) {
                $template_file = $tpl_path . __MODULE__ . '/' . __CONTROLLER__ . '/' . trim($template, '/');
            } else if ($pcount == 1) {
                $template_file = $tpl_path . __MODULE__ . '/' . trim($template, '/');
            } else {
                $template_file = $tpl_path . trim($template, '/');
            }
        } else {
            $template_file = $path . trim($template, '/');
        }
    }
    $template_file .= $tpl_suffix;
    if (!is_file($template_file)) {
        if ($show_error === true) {
            halt('模板文件不存在:' . $template_file);
        }
    } else {
        $cache_template = DATA_PATH . 'cache' . _DIR . 'template' . _DIR . __MODULE__ . _DIR . md5($template_file) . '.php';
        if (is_file($cache_template) && config('tpl_cache') == true && (config('tpl_expire') == 0 || (@filemtime($cache_template) + config('tpl_expire')) 
        		            	
            	            	
            	            	
            	
", $str);
        $str = preg_replace("/\{include\s+(.+)\}/", "", $str);        
        
        $str = preg_replace("/\{php\s+(.+?)\}/", "", $str);
        $str = preg_replace("/\{!(.+?)\}/", "", $str);
        $str = preg_replace("/\{if\s+(.+?)\}/", "", $str);
        $str = preg_replace("/\{else\}/", "", $str);
        $str = preg_replace("/\{elseif\s+(.+?)\}/", "", $str);
        $str = preg_replace("/\{\/if\}/", "", $str);
        //for
        $str = preg_replace("/\{for\s+(.+?)\}/", "", $str);
        $str = preg_replace("/\{\/for\}/", "", $str);
        //++ --         
        $str = preg_replace("/\{\+\+(.+?)\}/", "", $str);
        $str = preg_replace("/\{\-\-(.+?)\}/", "", $str);
        $str = preg_replace("/\{(.+?)\+\+\}/", "", $str);
        $str = preg_replace("/\{(.+?)\-\-\}/", "", $str);
        //loop
        $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/", "", $str);
        $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", " \\3) { ?>", $str);
        $str = preg_replace("/\{\/loop\}/", "", $str);
        //函数
        $str = preg_replace("/\{:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "", $str);
        $str = preg_replace("/\{~([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "", $str);
        //变量
        
        $str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "", $str);
        $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "", $str);
        $str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\->[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:])\}/", "", $str);
        $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|([^{}]*)\}/", "", $str);
        $str = preg_replace('/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\->)[^\}]+)\}/',"", $str);
        
        //数组
        $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "self::addquote('')", $str);
        $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\|([^{}]*)\}/es", "self::addquote('')", $str);

        //常量
        $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "", $str);
        //扩展标签
        $str = preg_replace("/\{tag:(\w+)\s+([^}]+)\}/ie", "self::tag('$1','$2')", $str);
        $str = preg_replace("/\{:tag:(\w+)\s+([^}]+)\}/ie", "self::tag('$1','$2',true)", $str);
        return $str;
    }

    public static function addquote($var) {
        return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
    }

    public static function tag($name, $data, $echo = false) {
        preg_match_all("/([a-z]+)\=[\"]?([^\"]+)[\"]?/i", stripslashes($data), $matches, PREG_SET_ORDER);

        foreach ($matches as $v) {
            if (in_array($v[1], array('action', 'cache', 'return'))) {
                $$v[1] = $v[2];
                continue;
            }

            $datas[$v[1]] = $v[2];
        }
        if (!isset($action) || empty($action)) { //方法
            return false;
        }
        if (isset($cache)) { //缓存
            $cache = intval($cache);
        } else {
            $cache = false;
        }
        if (!isset($return) || empty($return)) {
            $return = '$data';
        }
        $tag_file = EXT_PATH . 'tags' . _DIR . $name . '_tag' . EXT;
        if (!is_file($tag_file)) {
            return false;
        }
        $str = 'get("' . $cache_name . '");';
            $str .= 'if(' . $return . ' === false){';
            $str .= '$params = ' . self::filter_var($datas) . ';';
            $str .= '$tag = load_ext("tags/' . $name . '_tag",true);';
            $str .= $return . '=$tag->' . $action . '($params);';
            $str .= '$cache->set("' . $cache_name . '",' . $return . ',' . $cache . ');';
            $str .= '}';
            if ($echo == true) {
                $str .= 'echo ' . $return . ';';
            }
            $str .= ' ?>';
        } else {

            $str .= '$params = ' . self::filter_var($datas) . ';';
            $str .= '$tag = load_ext("tags/' . $name . '_tag",true);';
            if ($echo) {
                $str .= 'echo $tag->' . $action . '($params);';
            } else {
                $str .= $return . '=$tag->' . $action . '($params);';
            }
            $str .= ' ?>';
        }
        return $str;
    }

    protected static function filter_var($data) {
        $str = var_export($data, true);
        //$str = preg_replace('/\'\$(\w+?)\'/', "\$\\1", $str);
        $str = preg_replace('/\'/', '"', $str);
        $str = preg_replace('/\s{2,}/', '', $str);
        return $str;
    }

}
public function test(){ 
    include template();        
}
{if !empty($result)}
				{loop $result $r}
				
{$r['order_sn']}
					{$r['name']}
					{$r['mobile']}
					{:fdate($r['add_time'])}
					{if $r['status'] == 0}处理中{elseif $r['status'] ==1}已处理{else}无效{/if}
				
				{/loop}
				{else}
				

						您还没有收到任何预约
					
				
				{/if}