search
Homephp教程php手册模板引擎?No 正则而已

之前就觉得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')) < time())) {
            
        } else {
            template::template_compile($template_file, $cache_template);
        }
        return $cache_template;
    }
}
<?php

class template {

    /**
     * 编译模板
     * @param string  $template_file 模板文件
     * @param string $cache_file 缓存文件
     */
    public static function template_compile($template_file, $cache_file) {
        if (!is_file($template_file)) {
            return false;
        }
        $content = file_get_contents($template_file);
        $cache_dir = dirname($cache_file);
        if (!is_dir($cache_dir)) {
            mkdir($cache_dir, 0777, true);
        }
        $content = self::parse_template($content);
        @file_put_contents($cache_file, $content);
        return $cache_file;
    }

    public static function parse_template($str) {
        //include
        $str = preg_replace("/\{template\s+(.+)\}/", "<?php include template(\\1,'','','',false); ?>", $str);
        $str = preg_replace("/\{include\s+(.+)\}/", "<?php include \\1; ?>", $str);        
        
        $str = preg_replace("/\{php\s+(.+?)\}/", "<?php \\1?>", $str);
        $str = preg_replace("/\{!(.+?)\}/", "<?php \\1?>", $str);
        $str = preg_replace("/\{if\s+(.+?)\}/", "<?php if(\\1) { ?>", $str);
        $str = preg_replace("/\{else\}/", "<?php } else { ?>", $str);
        $str = preg_replace("/\{elseif\s+(.+?)\}/", "<?php } elseif (\\1) { ?>", $str);
        $str = preg_replace("/\{\/if\}/", "<?php } ?>", $str);
        //for
        $str = preg_replace("/\{for\s+(.+?)\}/", "<?php for(\\1) { ?>", $str);
        $str = preg_replace("/\{\/for\}/", "<?php } ?>", $str);
        //++ --         
        $str = preg_replace("/\{\+\+(.+?)\}/", "<?php ++\\1; ?>", $str);
        $str = preg_replace("/\{\-\-(.+?)\}/", "<?php ++\\1; ?>", $str);
        $str = preg_replace("/\{(.+?)\+\+\}/", "<?php \\1++; ?>", $str);
        $str = preg_replace("/\{(.+?)\-\-\}/", "<?php \\1--; ?>", $str);
        //loop
        $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\}/", "<?php \$n=1;if(is_array(\\1)) foreach(\\1 AS \\2) { ?>", $str);
        $str = preg_replace("/\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}/", "<?php \$n=1; if(is_array(\\1)) foreach(\\1 AS \\2 => \\3) { ?>", $str);
        $str = preg_replace("/\{\/loop\}/", "<?php \$n++;}unset(\$n); ?>", $str);
        //函数
        $str = preg_replace("/\{:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str);
        $str = preg_replace("/\{~([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php \\1;?>", $str);
        //变量
        
        $str = preg_replace("/\{\\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff:]*\(([^{}]*)\))\}/", "<?php echo \\1;?>", $str);
        $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/", "<?php echo \\1;?>", $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:])\}/", "<?php echo \\1;?>", $str);
        $str = preg_replace("/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\|([^{}]*)\}/", "<?php if(!empty(\\1)){echo \\1;}else{echo \\2;}?>", $str);
        $str = preg_replace('/\{(\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?:\->)[^\}]+)\}/',"<?php echo \\1;?>", $str);
        
        //数组
        $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\}/es", "self::addquote('<?php echo \\1;?>')", $str);
        $str = preg_replace("/\{(\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\|([^{}]*)\}/es", "self::addquote('<?php if(!empty(\\1)){echo \\1;}else{echo \\2;} ?>')", $str);

        //常量
        $str = preg_replace("/\{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)\}/s", "<?php echo \\1;?>", $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 = '<?php ';
        if ($cache !== false) {
            $cache_name =  'tag/'.$name.'_'.$action.  to_guid_string($datas);
            $str .= '$cache = cache::getInstance();';
            $str .= $return . '=$cache->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}
				<tr>
					<td>{$r['order_sn']}</td>
					<td>{$r['name']}</td>
					<td>{$r['mobile']}</td>
					<td>{:fdate($r['add_time'])}</td>
					<td>{if $r['status'] == 0}处理中{elseif $r['status'] ==1}已处理{else}无效{/if}</td>
				</tr>
				{/loop}
				{else}
				<tr>
					<td colspan="9">
						<span style="padding:10px; display:block;">您还没有收到任何预约</span>
					</td>
				</tr>
				{/if}
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PPT蒙版要怎么添加PPT蒙版要怎么添加Mar 20, 2024 pm 12:28 PM

关于PPT蒙版,很多人肯定对它很陌生,一般人做PPT不会将它吃透,而是凑活着可以做出来自己喜欢的就行,所以很多人都不知道PPT蒙版到底是什么意思,也不知道这个蒙版有什么作用,甚至更不知道它可以让图片变得不再那么单调,想要学习的小伙伴们快来了学习学习,为你的PPT图片上添上点吧PPT蒙版吧,让它不再单调了。那么,PPT蒙版要怎么添上呢?请往下看。1.首先我们打开PPT,选择一张空白的图片,之后右键点击【设置背景格式】,纯色选择颜色就行。2.点击【插入】,艺术字,输入字3.点击【插入】,点击【形状】

如何在 OneNote 中使用模板来提高工作效率如何在 OneNote 中使用模板来提高工作效率Apr 30, 2023 am 11:31 AM

您是否知道使用模板可以提高记笔记的速度以及捕捉重要想法的效率?OneNote有一套现成的模板供您使用。最好的部分是您还可以根据需要设计模板。无论您是学生、企业战士还是从事创造性工作的自由职业者。OneNote模板可用于以适合您风格的结构和格式记录重要笔记。模板可以是记笔记过程的大纲。业余爱好者只是做笔记,专业人士则在模板的帮助下通过结构良好的笔记做笔记并从中汲取联系。让我们看看如何在OneNote中使用模板。使用默认OneNote模板第1步:按键盘上的Windows+R。键入Oneno

C++ 模板特化的影响对于函数重载和重写C++ 模板特化的影响对于函数重载和重写Apr 20, 2024 am 09:09 AM

C++模板特化影响函数重载和重写:函数重载:特化版本可提供特定类型不同的实现,从而影响编译器选择调用的函数。函数重写:派生类中的特化版本将覆盖基类中的模板函数,影响派生类对象调用函数时的行为。

Flask-Bootstrap:为Flask应用程序添加模板Flask-Bootstrap:为Flask应用程序添加模板Jun 17, 2023 pm 01:38 PM

Flask-Bootstrap:为Flask应用程序添加模板Flask是一个轻量级的PythonWeb框架,它提供了一个简单而灵活的方式来构建Web应用程序。它是一款非常受欢迎的框架,但它的默认模板功能有限。要创建富有吸引力的用户界面,需使用其他框架或库。这就是Flask-Bootstrap的用武之地。Flask-Bootstrap是一个基于Twitter

PHP电子邮件模板:定制化和个性化您的邮件内容。PHP电子邮件模板:定制化和个性化您的邮件内容。Sep 19, 2023 pm 01:21 PM

PHP电子邮件模板:定制化和个性化您的邮件内容随着电子邮件的普及和广泛应用,传统的邮件模板已经不能满足人们对个性化和定制化邮件内容的需求。现在,我们可以通过使用PHP编程语言来创建定制化和个性化的电子邮件模板。本文将为您介绍如何使用PHP来实现这一目标,并提供一些具体的代码示例。一、创建邮件模板首先,我们需要创建一个基本的邮件模板。这个模板可以是一个HTM

Vue中如何实现图片的模板和蒙版处理?Vue中如何实现图片的模板和蒙版处理?Aug 17, 2023 am 08:49 AM

Vue中如何实现图片的模板和蒙版处理?在Vue中,我们经常需要对图片进行一些特殊的处理,例如添加模板效果或者加上蒙版。本文将介绍如何使用Vue实现这两种图片处理效果。一、图片模板处理在使用Vue处理图片时,我们可以利用CSS的filter属性来实现模板效果。filter属性给元素添加图形效果,其中的brightness滤镜可以改变图片的亮度。我们可以通过改变

C++模板和泛型的比较?C++模板和泛型的比较?Jun 04, 2024 pm 04:24 PM

C++中模板和泛型的区别:模板:编译时定义,明确类型化,效率高,代码体积小。泛型:运行时类型化,抽象接口,提供灵活性,效率较低。

C++ 模板的局限性和如何规避?C++ 模板的局限性和如何规避?Jun 02, 2024 pm 08:09 PM

C++模板的局限性及规避方法:代码膨胀:模板生成多个函数实例,可通过优化器、可变模板参数和编译时条件编译规避。编译时间长:模板在编译时实例化,可避免在头文件中定义模板函数、只在需要时实例化、使用PIMPL技术规避。类型擦除:模板在编译时擦除类型信息,可通过模板特化和运行时类型信息(RTTI)规避。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor