Using PHP as a template engine

后端 未结 6 2193
梦毁少年i
梦毁少年i 2020-12-02 00:57

I am not going to argue about the choice of a template engine against only PHP. I choose not to use a template engine, like Smarty, because I would like to learn how to prop

6条回答
  •  臣服心动
    2020-12-02 01:08

     function returnView($filename,$variables){
        ob_start();
            $htmlfile = file_get_contents($filename);  
            foreach($variables as $key=>$value){
              $htmlfile = str_replace("#".$key."#", $value, $htmlfile);
            }              
            echo $htmlfile;
        return ob_get_clean(); 
     } 
    
    //htmlfile
    
    #title#
    
    
    
    //usage
    
    echo returnView('file.html',array('title'=>'hello world!');
    

    im my framework i have function that loads view, and then outs it in layout:

     public function returnView(){
        ob_start();
        $this->loader();
        $this->template->show($this->controller,$this->action);
        return ob_get_clean(); 
     }
    

    Layout looks like this:

     
        
            <?php echo $this->layout('title'); ?>
        
        
            layout('content'); ?>
        
    
    

提交回复
热议问题