2

Ok - I've been looking at the MVC design pattern, and need to clarify the way it's supposed to work. I've done lots of reading, but as I'm new to DP's it's all fuzzy to me.

In an MVC, the model will handle my database work for me. That seems to be the simplest part.

The controller will handle the processing - dealing with forms, getting data from the model etc.

The problem for me is the view. Let's say I was looping through a resultset from a database query. When looping through the results, I might need to perform an if/else. Now, typically it seems that would be done within the View - but that seems counter intuitive to me. As soon as you need to start performing checks like that in the View, it seems the view is no longer just for presentation, but for processing logic just like the controller does. I also imagine that this could make for messy code within the View, but that's a different matter.

So, is the View an ok place to put small bit's of operation on the code, or is there somewhere else I should be putting that code? I know I could create a class method in a library somewhere else, but that seems like it would over complicate things un-needlessly.

I've tagged this with PHP as it's the primary language I work with.

5 Answers 5

2

ASP .NET MVC developer here, but I'm not sure that it matters given the nature of the question.

I try to think of a view as everything I need to render a particular operation. This will include stuff that is tied to specific business concepts, but also, will cover things like pagination and other concepts that aren't domain-specific problems, but still require some representation on the page.

Now, in terms of code operations, in some senses there isn't a lot you can do. You may have a part of your page that is dependent on, say, a collection being populated and having things in it. I have no real problem sticking in simple conditional constructs to handle things like this. It is code, but you could argue that its not important code. We aren't performing work per se, merely suppressing the display of certain elements based on the internal state of the view.

I'd argue that would apply to a lot of simple conditional display logic.

However, if you find that you are performing stuff that is more complex than that, it's probably best to change the model to give the view less work to do. e.g. if you're performing some nasty chained ternary in view-side markup, you may wish to consider encapsulating that logic in the construction of the model, instead of leaving it for the view to sort out.

So in conclusion, while simple on/off presentational constructs are generally okay (if a little messy) in views, consider creating extra properties etc on your model if you find your view code becoming inordinately complex or unmaintainable.

1

As a rule of thumb; in your view the only logic allowed is presentational logic. So this is perfectly allowed and you will encounter such logic in pretty much every MVC app;

<?php 
// assuming you have an array with data you want to display
foreach($array as $value):
    if($value < 0):
        $extrastyle = ' style="color: red"';
    else:
        $extrastyle = '';
    endif;
    ?>
    <span<?php echo $extrastyle; ?>><?php echo $value; ?></span>
<?php endforeach; ?>

But if you are ie. retrieving data from your model in your view, than you are doing stuff which is the controllers work. So don't be afraid to use some logic in your view, as long as it's logic to display data. All the preparation should be done in your controller. And if you doubt if the logic is logic to display data, make a choice based on your intuition.

-- note i purposely used the alternate notation (if(expr): and endif;) as it's kind of a convention to use this notation in views (using php). Offcourse you are very allowed to use standard notation. Further i would advise to use the ternary operator ($extrastyle = ($value < 0): ' style="color: red"':'';)

0

How code is distributed between view and controller very much depends on MVC implementation (framework) you are using.

Generally, it is definitely OK to have logic which has to do with presentation inside of the view, and not just limit the view to an actual template.

0

I think that you are applying too strict a definition for MVC. In the case of web pages you generally have two considerations for "controller": serverside or client side.

In your example where a condition is apparent (If this is the case do something, if not do something else) would generally be applied in the event that there is interaction, and that could trigger either client-side or server side conditions.

In addition it could be argued that the data itself could initiate a conditional context: if data is a then do b. This could be conditions inside the model.

The point is it's not entirely clear what you are doing.

0

In MVC Views must be as dumb as possible , Controllers should be skinny and Models fat.

If there is need to write an if/else statement in the view most MVC framework "Helpers" (for example ASP.NET has HtmlHelpers) which help hiding the conditional statement .

However , often whenever there are many conditional statements in a view it means that you are not following MVC pattern because the point of MVC is to separate responsibility so in those cases one should separate a view with such conditional statements into multiples views.

I suggest reading Jeff Atwood's article

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.