星罗

导航

模仿smarty迷你类代码

作业代码:

<?php

    class mini {
        public $template_dir="./view/";//模版目录

        public $compile_dir="./comp/";//编译目录

        public $_tpl_vars = array();//用来获取外界的变量

        //编译方法

       public function comp($temp) {

            $cont = file_get_contents($this->template_dir.$temp);

            $cont=str_replace('{$','<?php echo $this->_tpl_vars[\'',$cont);

            $cont =str_replace('}','\']; ?>',$cont);

            $fh=fopen($this->compile_dir.$temp.'.php','w');
            
            fwrite($fh,$cont);

            fclose($fh);

            Return $this->compile_dir.$temp.'.php';
        }

        //编译后展示
        public function display($temp) {

          
            
            $path =$this->comp($temp);
            
            include("$path");
        }

        //把外界的值传递到对象的属性上
        public function assign($key,$value=null) {
            
            //如果第一个参数是数组,则把该数组的单元加到_tpl_vars
            if(is_array($key)){
                $this->_tpl_vars=array_merge($this->_tpl_vars,$key);
            }else{
            $this->_tpl_vars[$key]=$value;
            }

            
            
    

        }
        

    }
   

?>

posted on 2013-03-05 21:17  星罗  阅读(142)  评论(0编辑  收藏  举报