+ -
当前位置:首页 → 问答吧 → 简单的模版

简单的模版

时间:2007-01-01

来源:互联网

这个设计有一个特性就是你可以在模版文件中直接写php代码
而编译后的代码将和你写的一模一样,
这个特性可是很有用的呀!
什么用处,哈如果你要在模版中操作session,cookie的话不用担心
类似其它模版会出现先有输出的问题
例如你可以这样写
复制内容到剪贴板
代码:
<?php
session_xxxxxx_code
.........
?>
<!---begin--->
xxxxxxx
<!---end--->
模版原代码,因为简单的很所以扩充起来也很方便.
复制内容到剪贴板
代码:
<?php
/*
*名称:     assets模版
*版本:     1.16
*作者:     achun
*email:    [email protected]
*版权:     BSD
*简单介绍:
*          根据axgle的assettemplate 改造的更精简的模版
*          其实就是自动加上烦人的echo<<<EOT 和 EOT;
*          吸取了assettemplate中$asset的技巧
*          因为更简单所以命名为assets
*
*/
class assets {

    /*
     *模版所在目录
     *加上..的目的很简单,../tpl在网站/目录的上层,这样用户就不能访问了.
     */
    var $tpl_dir = '../tpl';

    /*
     *道理和$tpl_dir一样,不过由于在我的设计里只有一个入口就是index.php
     *所有可以这样安排目录,如果你的需求不是这样就要改改了
     */
    var $cpl_dir= '../tpl_c';
    /*
     *asset的路径了,这个的作用axgle说的很好
     *不明白的还是找找axgle的文章吧.
     */
    var $asset_dir;
    function assets($dir='asset') {
        $this->asset_dir=$dir;
    }
    /*
     *整个模版的编译就在这里,就是字符串替换了
     *例子:
            <!---begin--->
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <script type="text/javascript" src="$asset/js/prototype.js" />
            <link rel="stylesheet" rev="stylesheet" href="/$asset/css/style.css" type="text/css" media="all" />
            <!---if($_SERVER['REMOTE_ADDR']=='127.0.0.1'){--->
            <title>本地访问</title>
            <!---}else{--->
            <title>远程访问</title>
            <!---}--->
            </head>
            <body>
            </body>
            </html>
            <!---end--->
     *说明:
     *      如果模版中有php的代码就必须用<!---begin--->和<!---end--->
     *      把它们包起来,所有的php代码都用<!---code--->的形式写
     *      注意{}的用法,switch也可以支持自己试试就知道具体的写法了.
     */
    function _compiled($tpl,&$cpl_file){
        $tpl_file=$this->tpl_dir."/$tpl";
        $cpl_file=$this->cpl_dir."/$tpl.htm";
        if(!file_exists($tpl_file)) return false;
        if(file_exists($cpl_file)){
            if(filemtime($cpl_file) >= filemtime($tpl_file)) return true;
        }
        $data=$this->_read($tpl_file);
        $data=str_replace('<!---begin--->','<?php'."\r\n".'echo<<<EOT',$data);
        $data=str_replace('<!---end--->',"\r\n".'EOT;'."\r\n".'?>',$data);
        $data=str_replace('<!---',"\r\n".'EOT;'."\r\n",$data);
        $data=str_replace('--->',"\r\n".'echo<<<EOT',$data);
        return $this->_write($cpl_file,$data);
    }
    function display($tpl){
        $cpl_file='';
        if(!$this->_compiled($tpl,$cpl_file)){
            return false;
        }
        extract((array)$this);
        $asset='$'.$this->asset_dir;
        include $cpl_file;
        return true;
    }
    function fetch($tpl) {
        ob_start();
        $this->display($tpl);
        $data=ob_get_contents();
        ob_end_clean();
        return $data;
    }
    function _read($filename,$mode="rb") {
        if($fp=@fopen($filename,$mode)) {
            flock($fp,LOCK_SH);
            $read_data=fread($fp,filesize($filename));
            fclose($fp);
        }
        return $read_data;
    }

    function _write($filename,$data,$mode="wb") {
        if($fp=@fopen($filename,$mode)) {
            flock($fp,LOCK_EX);
            fwrite($fp,$data);
            fclose($fp);
            return true;
        }
        return false;
    }
}
?>

作者: achun.shx   发布时间: 2007-01-01

作者: Nickboy   发布时间: 2007-01-01

作者: leehui1983   发布时间: 2007-01-01

:lol :lol :lol :lol

作者: kenus   发布时间: 2007-01-02

不错

作者: dzjzmj   发布时间: 2007-01-02

不知道是怎么弄,先顶

作者: shenstef   发布时间: 2007-01-11

作者: 特蓝克斯   发布时间: 2007-01-14

引用:
原帖由 特蓝克斯 于 2007-1-14 14:06 发表
顶,在这也见到你了呀~~~:D :D

作者: 绿竹居   发布时间: 2007-01-18

简单,高效模版

作者: phpvista   发布时间: 2007-03-02

求性能的,这个就是了

作者: phpvista   发布时间: 2007-03-02

在用正则改进一下就可以投入使用了

[ 本帖最后由 phpvista 于 2007-3-2 21:47 编辑 ]

作者: phpvista   发布时间: 2007-03-02

引用:
原帖由 phpvista 于 2007-3-2 21:44 发表
在用正则改进一下就可以投入使用了
我没有明白.
我的这个设计:
他直接就是支持的php原始码,所有php代码的正确性是由
使用者自己保证的,当然这个对于phper来说是没有问题的。
那我这个模板要正则做什么用呢?

作者: achun.shx   发布时间: 2007-03-03

热门下载

更多