Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load MVC views into main template file

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>

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?

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

like image 701
JasonDavis Avatar asked Sep 10 '11 00:09

JasonDavis


People also ask

How to Add a View MVC?

Right click the Views\HelloWorld folder and click Add, then click MVC 5 View Page with Layout (Razor). In the Specify Name for Item dialog box, enter Index, and then click OK. In the Select a Layout Page dialog, accept the default _Layout. cshtml and click OK.

What is view template MVC?

When you create an ASP.NET MVC application in Visual Studio, you have the option to create controllers and views that support data scaffolding. This walkthrough shows how to create a simple MVC application that takes advantage of the data templates for controllers and views that Visual Studio supports for MVC.

How to Create View in ASP net Core MVC?

Right-click on the Views folder, and then Add > New Folder and name the folder HelloWorld. Right-click on the Views/HelloWorld folder, and then Add > New Item. In the Add New Item - MvcMovie dialog: In the search box in the upper-right, enter view.


1 Answers

Something a little bit like

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

Then use with

$sidebarView = loadView('sidebar.php', array('stuff' => 'for', 'sidebar' => 'only');
$mainView = loadView('main.php', array('content' => 'hello',, 'sidebar' => $sidebarView);
like image 142
Joe Avatar answered Oct 11 '22 04:10

Joe