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?
View Files in MVC
Moderator: General Moderators
Re: View Files in MVC
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.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.
But if all you're going to do is immediately output the string, like
Code: Select all
$output = $this->executeView();
echo $output;-
Anthony Thomas
- Forum Newbie
- Posts: 3
- Joined: Sat Mar 10, 2012 4:38 pm
Re: View Files in MVC
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
Thanks
Re: View Files in MVC
You can do that just fine if the framework uses output buffering.
(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.
Code: Select all
function executeView() {
ob_start();
include($this->view);
return ob_get_clean();
}Your view can have anything you want and it looks like a normal PHP script, jumping between HTML/whatever and PHP any time.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: View Files in MVC
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.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.
(#10850)
Re: View Files in MVC
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)
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)