【问题标题】:Set included PHP and HTML content to a variable将包含的 PHP 和 HTML 内容设置为变量
【发布时间】:2011-01-10 19:39:34
【问题描述】:

我正在开发一个小型 MVC“框架”,它只会为我提供一个更有条理的工作空间,因为我发现我喜欢 CakePHP,但只是为了它的组织。现在,我正在尝试复制布局(其中包含 <?php echo $title_for_layout; ?><?php echo $content_for_layout; ?>,),我很好奇如何能够加载混合的 PHP 和 HTML 文件...保存通常会输出到的内容浏览器到一个变量...设置变量中提到的变量,然后输出布局变量并让PHP使用其中的变量来设置我想要的布局。

有什么想法吗?

【问题讨论】:

    标签: php model-view-controller cakephp layout


    【解决方案1】:

    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.

    【讨论】:

      猜你喜欢
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2021-06-08
      • 2016-10-23
      • 2011-03-08
      • 2019-05-08
      相关资源
      最近更新 更多