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

<?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';

?>

 

posted @ 2014-04-02 13:49  猿来如些  阅读(220)  评论(0编辑  收藏  举报