PHP中匿名函数的模板化和输出缓冲区的使用

PHP中匿名函数的模板化和输出缓冲区的使用,php,anonymous-function,output-buffering,Php,Anonymous Function,Output Buffering,我想我最好问一下,而不是在我的代码被审查时出丑 我正在制作一个网站,试图遵循MVC设计架构,虽然它甚至可能不是MVC,但我认为它工作得很好 现在我在做这样的事情, 我有一个render()函数,它接受字符串$template和参数数组 render($template, $vars=[]){ extract($vars); ob_start(); include($template.'.php'); ob_flush(); } 并输出视图。在控制器中,我会这样做

我想我最好问一下,而不是在我的代码被审查时出丑

我正在制作一个网站,试图遵循MVC设计架构,虽然它甚至可能不是MVC,但我认为它工作得很好

现在我在做这样的事情,

我有一个render()函数,它接受字符串$template和参数数组

render($template, $vars=[]){
    extract($vars);
    ob_start();
    include($template.'.php');
    ob_flush();
}
并输出视图。在控制器中,我会这样做

Class WhicheverController

    public static function defaultAction(){
        $article=Article::getOneByID(array('id'=>$_GET['id'], 'user'=>User::currentUser()));  //returns database result as object
        render('_header', array('title'=>$article->title));
        render('header', array('user'=>User::currentUser()));
        render('sidebar', array('user'=>User::currentUser()));
        $content=Comment::getByArticleID(array('article'=>$article->id));  //returns object
        //of root comments, highest level in comment-replies tree
        $comments=function() use ($content){
            foreach($content as $comment){
                $threads[]=function() use ($comment){
                    render('comment', array('comment'=>$comment));
                }
            }
            render('pagination', array('content'=>$threads)); //pagination paginates based on number of
            //items in content, and echoes it or runs it if it is_callable
        }
        render('article', array('article'=>$article, 'comments'=>$comments));
    }
最后是article.php

//some html with echoing $article->variablethis or variablethat
<div class="comments">
    <?= $comments(); ?>
</div>
$user\u articles
然后发送到

echo View::render('user_page', array('articles'=>$user_articles, 'profile'=>$profile, 'comments'=>$user_comments));
如您所见,我也对注释执行此操作,因此代码变得冗长。我不想改变任何东西,但我没有破坏任何东西,尽管我觉得这在某种程度上违反了DRY(?)


有没有更好的方法可以做到这一点,既不牺牲清晰度,又能使其更加紧凑?

使用
返回ob\u get\u clean()
而不是
ob\u flush()
,您可以存储和构建输出。render函数也不应该是全局的,它应该是视图类的一部分。@Lawrence所以对于每个视图,我都会这样做,比如说,$sidebar=newview();,构建变量,如$sidebar->user,以及呈现变量,如$sidebar->render('sidebar')?为控制器创建基类define方法让我们说
getComponent
,然后调用让我们说
createComponentPagination
,这将在
中定义,其中vercontroller
,将pagination定义为组件,使用自己的呈现函数,将模板作为函数,并将控制器本身传递给它们,然后您可以在模板中调用类似的内容:
$controller->getComponent('pagination')->render()
@Kazz我定义了一个新的
PaginationView
,它获取模型数据并进行渲染。问题是,有时(实际上,大多数时候)我需要预渲染数据以向其传递其他变量,所以我还创建了PartialView,它获取内容、模板文件和变量,并返回一个包含我要分页的部分视图的数组。现在看起来确实更干净了,但我必须先做
$something=newpartialview
,然后再做
$articles=newpaginationview($something)
,这看起来仍然很难看。该代码是功能,因为它是,但我希望它更干净。这是一种过早优化的情况吗?
$content=Article::getByAuthorID(array('author'=>$profile->id), array('order'=>$order,'column'=>'time_submitted'));
$user_articles=new PartialView('_article', $content, array('length'=>60));
$user_articles=new PaginationView($user_articles->views, 10, 1);
echo View::render('user_page', array('articles'=>$user_articles, 'profile'=>$profile, 'comments'=>$user_comments));