开发者

Set included PHP and HTML content to a variable

I am working on a small MVC "framework", which will simply provide a more organized workspace for me, as I've found I love CakePHP but only for it's organization. Now, I'm trying to replicate the layouts (which contain <?php echo $title_for_layout; ?> and <?php开发者_开发问答 echo $content_for_layout; ?>,) and I'm curious how I would be able to load a mixed PHP and HTML file... save what would normally be outputted to the browser to a variable... set the variables mentioned inside the variable, and then output the layout variable and have PHP use the variables inside it to setup the layout exactly how I want it.

Any ideas?


Capture the buffer output to a string with the output buffer control functions.

$string = getIncludeContents('template.php');

function getIncludeContents($filename) {
    if (is_file($filename)) {
        ob_start();
        include $filename;
        $contents = ob_get_contents();
        ob_end_clean();
        return $contents;
    }
    return false;
}

Example taken from.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜