如何将MVC视图加载到主模板文件中

Jas*_*vis 8 php model-view-controller templates

我正在研究自己的MVC框架.下面是我到目前为止的示例控制器.

我有办法将模型加载到我的控制器中,也可以查看文件.

我想为我的网站提供不同的模板选项.我的模板只是一个页面布局,它将从我的控制器创建的视图插入到模板文件的中间.

/**
 * Example Controller
 */
class User_Controller extends Core_Controller {

    // domain.com/user/id-53463463
    function profile($userId)
    {
        // load a Model
        $this->loadModel('profile');  

        //GET data from a Model
        $profileData = $this->profile_model->getProfile($userId);

        // load view file and pass the Model data into it
        $this->view->load('userProfile', $profileData);
    }

}
Run Code Online (Sandbox Code Playgroud)

这是模板文件的基本概念......

DefaultLayout.php

<!doctype html>
<html lang="en">
<head>
</head>
<body>



Is the controller has data set for the sidebar variable, then we will load the sidebar and the content
<?php if( ! empty($sidebar)) { ?>

<?php print $content; ?>

<?php print $sidebar; ?>


If no sidebar is set, then we will just load the content
<?php } else { ?>

<?php print $content; ?>

<?php } ?>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

没有任何页眉,页脚和其他任何内容的另一个模板可用于AJAX调用

EmptyLayout.php

<?php
$content
?>
Run Code Online (Sandbox Code Playgroud)

我正在寻找关于如何加载我的主模板文件,然后包含和查看文件到我的主布局文件的内容区域的想法?

在示例布局文件中,您可以看到内容区域有一个名为$ content的变量.我不确定如何使用视图内容填充它,以插入到我的主布局模板中.如果您有任何想法,请发布样品

Joe*_*Joe 12

有点像

function loadView ($strViewPath, $arrayOfData)
{
// This makes $arrayOfData['content'] turn into $content
extract($arrayOfData);

// Require the file
ob_start();
require($strViewPath);

// Return the string
$strView = ob_get_contents();
ob_end_clean();
return $strView;
}
Run Code Online (Sandbox Code Playgroud)

然后使用

$sidebarView = loadView('sidebar.php', array('stuff' => 'for', 'sidebar' => 'only');
$mainView = loadView('main.php', array('content' => 'hello',, 'sidebar' => $sidebarView);
Run Code Online (Sandbox Code Playgroud)