php视图处理类

文章详细描述了如何在PHP视图模板中使用变量、for循环、foreach循环以及if-else条件判断,展示了`View`类中的相关函数实现,以动态渲染HTML内容。
摘要由CSDN通过智能技术生成

#简介

  • 视图模版替换
  • 支持变量,for循环,foreach循环,if判断,if else判断

#调用

  1. index.php
    <?php
    use ppt\tool\View;
    $haha = 12;
    $id = 111;
    $arr = ['x','y',3];
    $pp = [
        'x'=>['id'=>'1','name'=>'12'],
        'y'=>['id'=>'1','name'=>'12'],
        'z'=>['id'=>'1','name'=>'12'],
    ];
    echo View::display('',['haha'=>$haha,'id'=>$id,'arr'=>$arr,'pp'=>$pp,]);
    echo '123456';
    
    
    
  2. index.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    {@if($id===1)}
    1
    {@/if}
    {@else if($id===2)}
    2
    {@/else if}
    {@else}
    other
    {@/else}
    {@foreach($arr as $item)}
    {@$item}
    {@/foreach}
    </body>
    </html>

输出tmp.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<?php if(($id===1)){ ?>
1
<?php } else if($id===2){ ?>
2
<?php } else { ?>
other
<?php } foreach($arr as $item){ echo $item; } ?>
</body>
</html>

#代码View.php

<?php

namespace ppt\tool;
class View
{
    private static $tpl_path = app . '/html/';
    private static $ext = '.html';

    public static function display($_tpl = '', $_param = [],$_format=true)
    {
        if (!empty($_param)) {
            foreach ($_param as $_key => $_v) {
                $$_key = $_v;
            }
        }
        if (empty($_tpl)) {
            switch (run_mode){
                case 'file':
                    $_tpl =  debug_backtrace()[0]['file'];
                    break;
                case 'namespace':
                    $_tpl =  str_replace('.php','\\',debug_backtrace()[0]['file']).debug_backtrace()[1]['function'].'.php';
                    break;
            }
            $_tpl = preg_replace('/^'.preg_quote(root,'/').'/','root',$_tpl);
            $_tpl = strtolower($_tpl);
            $_tpl = preg_replace('/^root/',root,$_tpl);
            $_tpl = str_replace('\\','/',$_tpl);
            $_tpl = preg_replace('/^'.preg_quote(app,'/').'\/code\//',app . '/html/',$_tpl);
            $_file1 = str_replace('.php',self::$ext,$_tpl);
        }else{
            $_file1 = self::$tpl_path.$_tpl.self::$ext;
        }
        $_file2 = self::$tpl_path . 'tmp' . self::$ext;
        if(is_file($_file2))
            unlink($_file2);
        /*if (!is_file($_file1)) {
            $_msg = 'error: 文件' . __FILE__ . ' 第' . __LINE__ . '行 函数' . __FUNCTION__ . ' file【' . $_file1 . '】不存在';
            echo $_msg;
            exit();
        }*/
        $_html = file_get_contents($_file1);
        $_content = self::biuBiu($_html);
        file_put_contents($_file2, $_content);
        ob_start();
        require_once $_file2;
        $_res = ob_get_clean();
//        unlink($_file2);
        if($_format===true)
            $_res = HtmlFormat::fomatHtml($_res);
        else
            $_res = HtmlFormat::miniHtml($_res);
        return $_res;
    }

    public static function biuBiu($tpl)
    {
        $content = self::fanyiIf($tpl);
        $content = self::fanyiElse($content);
        $content = self::fanyiXunhuan($content);
        $content = self::fanyiBianliang($content);
        $content = self::biaoqianQuchon($content);
        return $content;
    }
    private static function biaoqianQuchon($tpl)
    {
        $pattern = "/[\s]*\?\>[\s]*\<\?php /U";
        preg_match_all($pattern, $tpl, $res);
        if (!empty($res)) {
            foreach ($res[0] as $item) {
                $tpl = str_replace($item, ' ', $tpl);
            }
        }
        return $tpl;
    }

    private static function fanyi($tpl,$preg,$replace_preg,$replace_arr)
    {
        preg_match_all($preg, $tpl, $res);
        if (!empty($res)) {
            $res_p = $res[0];
            foreach ($replace_preg as $pk=>$pv){
                $res_p = preg_replace($pk,$pv,$res_p);
            }
            foreach ($replace_arr as $ak=>$av){
                $res_p = str_replace($ak,$av,$res_p);
            }
            foreach ($res[0] as $key => $item) {
                $tpl = str_replace($item, $res_p[$key], $tpl);
            }
        }
        return $tpl;
    }

    private static function fanyiElse($tpl)
    {
        $tpl = self::fanyiElse_head($tpl);
        $tpl = self::fanyiElse_end($tpl);
        return $tpl;
    }

    private static function fanyiElse_head($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*else[\s\S]*[\s]*\}/U',['/[\s]+/'=>'', '/\{@else/U'=>'<?php else ', '/\}/U'=>'{ ?>'],[]);
    }

    private static function fanyiElse_end($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*\/[\s]*else[\s\S]*[\s]*\}/U',['/[\s]+/'=>'', '/{@\/else[\s\S]*\}/U'=>'<?php } ?>',],[]);
    }

    private static function fanyiIf($tpl)
    {
        $tpl = self::fanyiIf_head($tpl);
        $tpl = self::fanyiIf_end($tpl);
        return $tpl;
    }

    private static function fanyiIf_head($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*if[\s\S]*[\s]*\}/U',['/[\s]+/'=>'',],['{@if'=>'<?php if(', '}'=>'){ ?>',]);
    }

    private static function fanyiIf_end($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*\/[\s]*if[\s]*\}/',['/[\s]+/'=>'',],['{@/if}'=>'<?php } ?>',]);
    }

    private static function fanyiBianliang($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*\$[\S]+[\s]*\}/U',['/[\s]+/'=>'',],['{@'=>'<?php echo ', '}'=>'; ?>',]);
    }

    private static function fanyiXunhuan($tpl)
    {
        $tpl = self::fanyiXunhuan_head($tpl);
        $tpl = self::fanyiXunhuan_end($tpl);
        return $tpl;
    }

    private static function fanyiXunhuan_head($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*for[\s\S]*[\s]*\}/U',[
            '/[\s]+/'=>' ',
            '/\{[\s]+/U'=>'{',
            '/[\s]+\}/U'=>'}',],
            [
                '{@for'=>'<?php for',
                '}'=>'{ ?>',
            ]);
    }

    private static function fanyiXunhuan_end($tpl)
    {
        return self::fanyi($tpl,'/\{@[\s]*\/[\s]*for[\s\S]*[\s]*\}/U',['/[\s]+/'=>'', '/\{@\/for[\s\S]*\}/'=>'<?php } ?>',],[]);
    }
}

#终结

  • 简单几十枚
  • 10
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

etafort

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值