How to load MVC views into the main template file

I am working on my own MVC infrastructure. Below is an example of the controller that I have.

I have a way to load models into my controller, as well as view files.

I also want to have different template options for my site. My template will be just a page layout that inserts the views 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 the basic idea of ​​a 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 a header, footer, or anything else can be used for AJAX calls

 EmptyLayout.php <?php $content ?> 

I'm looking for ideas on how to upload my main template file and then include and view files in 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'm not sure how I can fill this content with content so that it fits into my main layout template. If you have any ideas, send a sample.

+7
source share
1 answer

Something a 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

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

All Articles