php的include功能实现:用于在内存合并文件而非读入替换再存盘再载入使用include

----------code-----------

<?php
if (empty($_GET['tpl'])) exit('?tpl=temp name');

$html = insert(tpl($_GET['tpl']));
$html = str_replace('{CSS_PATH}', 'http://phpcms.q/statics/css/', $html);
$html = str_replace('{IMG_PATH}', 'http://phpcms.q/statics/images/', $html);
$html = str_replace('{JS_PATH}', 'http://phpcms.q/statics/js/', $html);
echo $html;

function insert($file) {
    $txt = get_include_contents($file);    
    $split = preg_split("/(\{template +[\"'][^\"']+[\"'] *, *[\"'][^\"']+[\"']\})/", $txt, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);
    
    if (count($split) > 1) {
        foreach ($split as $key => $tmp) {
            if (preg_match("/\{template +[\"'][^\"']+[\"'] *, *[\"']([^\"']+)[\"']\}/", $tmp, $match)) {
                $split[$key] = insert(tpl($match[1]));
            }
        }
        
        return implode('', $split);
    } else {
        return $txt;
    }
}

function tpl($name) {
    $name = './phpcms/templates/chrd/content/'.$name.'.html';
    if (! file_exists($name)) {
        exit('包含出错,文件不存在:' + $name);
    }
    
    return $name;
}

function get_include_contents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

------------说明-------

只要是为了方便按phpcms的模板存放安排显示模板的html效果,而不需要使用平台的生成功能后再查看生成页,这个功能实现就很容易的对include和某些变量自己写解释功能.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值