0
我做了一个小的MVC框架,但我需要我的视图类可以返回字符串的页面,但在此之前,应用所有的变量。如何才能做到这一点?RenderToHtml视图文件并返回一个字符串?
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;
}
}
控制器:
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)
}
所以htmlview会定期indexView部分
是的,这会工作,但包括这个 - $>文件仍将包括在父视图中查看文件,所以它会显示在2个 – 2010-11-22 11:06:18
通行证参数'$ html-> renderToHtml($ ob = true);'所以可以决定什么时候做ob_start()? – ajreal 2010-11-22 11:11:44
我用ob_get_clean()而不是ob_get_flush(),它的工作原理!谢了哥们 – 2010-11-22 11:19:57