Viewfile RenderToHtml ve bir dize döndürür?

0 Cevap php

Ben küçük bir MVC Framework yapılmış, ama benim bakış Sınıf dizge olarak sayfaya geri dönmek ama bundan önce tüm değişkenleri uygulayabilirsiniz gerekir. Bu nasıl yapılabilir?

class View{
    private $registry;
    private $vars = array();
    private $file;
    public function __set($index, $value){
        $this->vars[$index] = $value;
    }

    public function  __construct($controller, $action){
        $this->file = "Views/".$controller."/".$action.".php";
        $this->registry = new Registry();
    }

  public function renderToHtml(){
        try{
            if(file_exists($this->file)){

                foreach ($this->vars as $key => $value){
                   $$key = $value;
                }

                /** include $this->file; **/ 
//apply vars on file 
                return $html;
            }else{
                throw new Exception("No such View file");
            }
        }catch (Exception $e) {
            echo '<h1>Caught exception: ',  $e->getMessage(), "</h1>";
            exit;
        }
    }

Denetleyici:

public function indexAction(){
   $html = new View('SomeController', 'htmlview');
   $html->somevar = "blabla";
   $this->View->fubar = $html->renderToHtml();
   $this->View->render() //this will render the current action/view (index) 
}

böylece HtmlView normal indexView kısmi olacak

0 Cevap