模板解析原理,脱裤子放屁

<?php
    
    $datalist = array(
                    '11',
                    '11',
                    '11',
                    '11',
                    '11',
                    '11',
                    );
        
        $hello = "模板测试";

function compile($direct,$source){
    if(!file_exists($direct)){    
        $con = file_get_contents($source);
        
        $con = preg_replace("/[\n\r\t]*\{loop\s+(\\$[-\w]+)\s+(\\$[-\w]+)\}[\n\r\t]*/","<?php if(is_array(\\1)): foreach(\\1 AS \\2): ?>",$con);
        
        $con = preg_replace("/[\n\r\t]*\{loop\s+(\\$[-\w]+)\s+(\\$[-\w]+)\s+(\\$[-\w]+)\}[\n\r\t]*/","<?php if(is_array(\\1)): foreach(\\1 AS \\2 => \\3): ?>",$con);
        
        $con = preg_replace("/[\n\r\t]*\{\/loop\}[\n\r\t]*/i","<?php endforeach;endif; ?>",$con);
            
        $con = preg_replace("/[\n\r\t]*\{\s*if\s+(\\$[-\w]+)\s*([=><]{0,3})\s*(\\$[-\w]+)\}[\n\r\t]*/i","<?php if(\\1 \\2 \\3): ?>",$con);
        
        $con = preg_replace("/[\n\r\t]*\{\s*endif\s+(\\$[-\w]+)\s*([=><]{0,3})\s*(\\$[-\w]+)\}[\n\r\t]*/i","<?php if(\\1 \\2 \\3): ?>",$con);
            
        $con = preg_replace("/[\n\r\t]*\{(\\$\w+)\s*\}[\n\r\t]*/","<?php echo \\1 ;?>",$con);
        
        $con = preg_replace("/[\n\r\t]*\{if\s+([-\S]+)\}[\n\r\t]*/","<?php if( \\1): ?>",$con);
        
        $con = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/","<?php else: ?>",$con);
        
        $con = preg_replace("/[\n\r\t]*\{\/if\}[\n\r\t]*/","<?php endif; ?>",$con);
        
        file_put_contents($direct,$con);
    }    
    
}
compile('index.tpl.php','loop.htm');


include 'index.tpl.php';

?>

 

转载于:https://www.cnblogs.com/wenzhilin/p/3640523.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值