Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Lucas Krause
Lucas Krause
19,924 Points

Should the View code *return* or *print* the produced HTML markup?

The Controller Code gets relevant data from the Model code and passes that data to the View code, which, in turn, produces HTML markup and returns that markup to the controller which echoes it. Right? Or would it be better to have the View code produce the markup and immediately print it?

2 Answers

Tom Nulty
Tom Nulty
2,017 Points

Hi again Lucas :)

Since the whole point of the echo statement is to display things on screen. I would have to say that it would be best to put the echo in the view section and have the function be called from the controller. The controller is there to bring both the model and view together so at this sort of introduction level there should only really be function calls in controller.

Hope that helps :)



You can think of the view as a 'terminating stop'. It's the output from all the inner workings - the functions, variables, logic - it's whatever you're going to present to the user.

The view doesn't return any data to the controller - at least (I think) not in the sense you're talking about.

it's actually the opposite to your description - the controller calls a view, and straight away that view does it's job of displaying data to the user. You pass information into the view for display e.g. variables etc you might need.

The view might call the controller if you submit a form/click a link. You would be calling the application again and that request would travel once again through the entire application.

When it comes to views - the method in which you display data should depend on the data itself.

If you're planning to include a lot of html in your view, it is probably advisable to not use echo statements for this! Instead you would open and close php variables at points you want to display dynamic data. For instance, you have an 'account' page and you want the h1 tag to display the name of your user:

<h1>Welcome back <?php echo $username; ?>!</h1>

If you're creating html dynamically i.e. you have a list of links or buttons available to the user, it might be easier to create a variable to hold your html and echo that variable to the screen:


$html = '<ul>';

foreach ($people as $person) {
    $html .= '<li>;
    $html .= $person;
    $html .= '</li>;

$html .= '</ul>';

echo $html;

Instead of building a variable like this, you could also just use echo commands in place of the $html - both would give you the same output.

To answer your question - use the method which is the most readable, requires the least code, lowest maintenance, the most sensible!

I hope this answers your question, let me know if you need any more help.