Page 1 of 1

View Files in MVC

Posted: Sat Mar 10, 2012 4:58 pm
by Anthony Thomas
Hi Guys, it's my first post here, I know a little about php and programming but this is confusing me a little.
I did do a quick search but there's tons of different stuff on this, it's quite overwhelming.

I've been creating a small app in a MVC structure, however I'm confused on how to format the view files/classes.

Currently a controller gets data from the model, the controller checks this data or whatever, then passes it to the view class for displaying it. I've been told that the view class should have a function that generates the HTML markup for the whole page and concatenates it into a php string, then a function to just echo the string out.

Sorry if this has been covered before, as I'm sure after just re-reading it now, it probably sounds a bit stupid to most people?

Re: View Files in MVC

Posted: Sat Mar 10, 2012 6:42 pm
by requinix
Anthony Thomas wrote:I've been told that the view class should have a function that generates the HTML markup for the whole page and concatenates it into a php string, then a function to just echo the string out.
It doesn't have to be that exactly. The framework could use output buffering and the view would just act like a normal page. But in essence, yes: there are advantages to executing the view before it would be rendered, and that requires you stick the view's output somewhere like into a string.

But if all you're going to do is immediately output the string, like

Code: Select all

$output = $this->executeView();
echo $output;
then it's pointless.

Re: View Files in MVC

Posted: Sun Mar 11, 2012 6:18 am
by Anthony Thomas
Coming from a front-end development background, I would prefer to have a php file of HTML etc... with embedded PHP functions/loops to display the data passed to it. What are the dis-advantages of doing this, and how would you implement it? eg the controller checks the data passed from the model, then a function in the controller 'requires' the php/html file so that it can use the data?

Thanks

Re: View Files in MVC

Posted: Sun Mar 11, 2012 2:30 pm
by requinix
You can do that just fine if the framework uses output buffering.

Code: Select all

function executeView() {
	ob_start();
	include($this->view);
	return ob_get_clean();
}
(it could easily be more complicated, but that's the gist of it)

Your view can have anything you want and it looks like a normal PHP script, jumping between HTML/whatever and PHP any time.

Re: View Files in MVC

Posted: Fri Mar 30, 2012 3:16 pm
by Christopher
Anthony Thomas wrote:I've been told that the view class should have a function that generates the HTML markup for the whole page and concatenates it into a php string, then a function to just echo the string out.
requinix gives really good advice and examples. There is no requirement on how this is done in PHP. Including a PHP template and capturing it with output buffering is a general best practice in PHP and most frameworks do exactly that.

Re: View Files in MVC

Posted: Tue Apr 03, 2012 4:30 am
by kon
Anthony there is many MVC implementations. That is something good because you can choose what best fits in your programming view, your experiences or what a programmer you like to be in the future. I rarely see MVC implementations that have no logic (to me) , so there are many alternative ways. So here is my opinion (and implementation I use) about views. Most often an application has few final views , this can be expressed as template files that uses geter methods of the current controller (u can use inheritance and interfaces to insure that the controllers have the methods defined by the final template).
There is also one more layer in View, the View Generator Classes , that takes an object (or a list of them) and generate html code for them. Controller can use them to set variables that the final template will use with getters methods. As said this just one approach… (there is also a third hybrid layer in this approach but not need to make it more complicated)