RenderToHtml 视图文件并返回字符串?

发布于 2024-10-03 17:26:02 字数 1328 浏览 1 评论 0原文

我制作了一个小型 MVC 框架,但我需要我的视图类可以将页面作为字符串返回,但在此之前应用所有变量。这怎么能做到呢?

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 的一部分

I made a small MVC Framework, but i need that my view Class can return the page as string but before that apply all the variables. How can this be done?

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;
        }
    }

Controller:

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) 
}

so the htmlview will be a partial in regular indexView

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-10-10 17:26:02

使用 输出缓冲

示例:

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

Use output buffering

Example:

ob_start();
/* your error catch */
$render_html = ob_get_flush();
return $render_html;
只有一腔孤勇 2024-10-10 17:26:02

看起来您需要获取文件内容并替换其中的所有变量。

Looks like you nee to get the file contents and replace all the variables there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文