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


How to load MVC views into main template file

我正在开发自己的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);
    }
}

这是模板文件的基本思想…

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>

另一个模板,没有任何页眉,页脚,其他任何东西,可以用于AJAX调用

EmptyLayout.php
<?php
$content
?>

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

在示例布局文件中,您可以看到内容区域有一个名为$content的变量。我不知道如何用视图内容填充,插入到我的主布局模板中。如果您有任何想法,请张贴样本

有点像

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

然后与

连用
$sidebarView = loadView('sidebar.php', array('stuff' => 'for', 'sidebar' => 'only');
$mainView = loadView('main.php', array('content' => 'hello',, 'sidebar' => $sidebarView);