如何将MVC视图加载到主模板文件中
我正在开发自己的MVC框架.下面是我到目前为止拥有的示例控制器.
I am working on my own MVC framework. Below is an example controller I have so far.
我有一种将模型加载到控制器中并查看文件的方法.
I have a way of loading models into my controller and also view files.
我还希望为我的网站提供不同的模板选项.我的模板将只是一个页面布局,该页面布局会将从控制器创建的视图插入模板文件的中间.
I am wanting to also have different template options for my site. My template will just be a page layout that inserts the views that are created from my controller into the middle of my template file.
/**
* 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);
}
}
这是模板文件的基本概念...
Here is a basic idea of the template file...
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调用
Another Template without any header, footer, anything else, can be used for AJAX calls
EmptyLayout.php
<?php
$content
?>
我正在寻找有关如何加载主模板文件,然后将文件包含并查看到主布局文件的内容区域中的想法?
I am looking for ideas on how I can load my main template file and then include and view files into the content area of my main layout file?
在示例布局文件中,您可以看到内容区域具有一个名为$ content的变量.我不确定如何将视图内容填充到主布局模板中.如果您有任何想法,请发布示例
In the sample layout file, you can see that the content area has a variable called $content. I am not sure how I can populate that with the views content, to be inserted into my main layout template. If you have any ideas, please post sample
有点像
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);