Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle templates for MVC websites?

I have this marked as PHP but only because I'll be using PHP code to show my problem.

So I have some code like this for the controller:

switch ($page)
{
    case "home":
        require "views/home.php";
        break;
    case "search":
        require "views/search.php";
        break;
}

Obviously there's more pages but this should illustrate my issue. There is a common header, navigation and footer for both of these pages (and for all pages on the site for that matter). Should I be using multiple require statements? My first guess would be:

switch ($page)
{
    case "home":
        require "templates/header.php";
        require "templates/navigation.php";
        require "views/home.php";
        require "templates/footer.php";
        break;
    case "search":
        require "templates/header.php";
        require "templates/navigation.php";
        require "views/search.php";
        require "templates/footer.php";
        break;
}

Somehow my gut tells me this isn't correct.

like image 376
Andrew G. Johnson Avatar asked Dec 01 '08 01:12

Andrew G. Johnson


People also ask

What is MVC template used for?

Model–view–controller (MVC) is a software architectural pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements.


1 Answers

The controller should just set up the data for the view and choose which view to display. The view should be responsible for the layout of the page, including shared pages. I like your first sample over the second.

like image 163
tvanfosson Avatar answered Oct 12 '22 23:10

tvanfosson