如何将MVC视图加载到主模板文件中 [英] How to load MVC views into main template file

查看:96
本文介绍了如何将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);

这篇关于如何将MVC视图加载到主模板文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆