Jump to content

Recommended Posts

I'm looking for some advice on the best ways to handle the "view" part of the MVC pattern, specifically the loading of the view files, dependent on where you are in the system, what actions you've called, what the system requires, etc...

 

In my previous projects, I've gone about this generally in two different ways.

 

Method A:

 

Example URL: mysite.com/report/view/1

 

Flow:

 

  • Various routing & file including occurs
  • Eventually this calls ReportController->view(1) which does whatever it needs to do..loads up a Report model with parameter "1", etc... 
  • Within the view() method we set the variables that we want to use in the ReportTemplate
  • On __destruct of that controller object it calls Render() on the ReportTemplate object
  • This Render method looks in "modules/reports/views" for 3 files: "header.html", "footer.html", <action>.html (in this case "view.html"
  • If it can't find the <action> view, it looks for "index.html"
  • If it can't find any of those files it loads the global files
  • This way it automatically looks for a specific view for whatever module & action we have called

 

Downsides to this include:

  • Fairly limited in the layout as always have to have header, <action>, footer unless we override the Render() method for particular Templates, which isn't that big of a deal, just seems inelegant
  • Might want to call a different template rather than just rely always on working it out from the querystring

 

Method 2):

 

Similar to Method A, but rather than having seperate template classes for each module and an overall Render() function that works the views out by itself, we manually load whatever templates we want, eg:

 

  • ReportController->view(1)
  • Does stuff
  • Creates new Template object
  • Template->set("somevar", $somevar)->set("var2", $var2)->etc...
  • Template->load(/path/to/chosen/template)
  • Template->display()
  • Template->load(/path/to/some/other/template)
  • Template->display()
  • etc...

 

Downsides of this include having to manually do it all the time. But it does allow to load whatever template/view you want, whenever you want.

 

 

I'm aware neither of these is probably a particular good way to do it, so I was wondering what the best practices would be to actually handle the loading of views/templates in real-world professional systems.

 

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/277370-handling-viewstemplates-in-mvc/
Share on other sites

The two ideas are both valid. Generally though, you would fall only through to an auto resolving mechanism if nothing was setup within a controller manually.

 

As for your header / footer issue. It's easy enough to implement inheritance within templating. You will find most modern php template engines already do so.

 

This enables you to extend one template with another at the template level.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.