2010-11-22 13 views
0

Küçük bir MVC Framework yaptım, ancak görünüm Sınıfımın sayfayı bir dize olarak döndürmesine, ancak daha önce tüm değişkenleri uygulamasına ihtiyacım var. Bu nasıl yapılabilir?RenderToHtml viewfile ve bir dize döndürmek?

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: Eğer dosya içeriğini almak ve orada tüm değişkenleri yerine nee gibi

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

cevap

1

Kullanım output buffering

Örnek:

ob_start(); 
/* your error catch */ 
$render_html = ob_get_flush(); 
return $render_html; 
+0

Evet bu çalışacaktır ancak $ this-> o 2x –

+0

Geçiş gösterilir böylece dosya hala ana görünümünde görünüm dosyasını içerecektir dahil $ html-> renderToHtml ($ ob = true) 'a bir paramter; ob_start() ne zaman karar verebilir? – ajreal

+0

ob_get_flush() yerine ob_get_clean() kullanılır ve işe yarıyor! Teşekkürler dostum –

0

görünüyor düzenli indexView kısmi olacaktır.

İlgili konular