views:

106

answers:

3

From the limited amount of people that I've talked to regarding MVC web frameworks, I've heard people say that, forgetting about forms, a view file should ideally contain HTML markup, string manipulation and a few for each loops. I've also been told that if statements in views should be avoided if at all possible. Is this generally agreed?

EDIT: The situation that has inspired this question is writing a navigation, I'm finding myself writing:

if secondary_navigation_item has children
    ...

I'm thinking, ideally, does this qualify as logic (that should not be here)?

+2  A: 

A view should basically contain:

  • HTML Markup
  • Javascript
  • CSS
  • Minimum of server-side code you may need to put into view

So, a View should typically contain the layout elements. The main processing logic should go in Controller.

More Info:

http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

Sarfraz
Perhaps I'm missing your point here but Javascript? CSS? Strewth, I don't do that on my static HTML projects. I'd hardly say that was best-practice imo. Though it does have it's place.
Dr. Frankenstein
I agree with Sarfraz. Its perfectly acceptable to loop through an array, use 'if' to determine which image to display, etc. Try to keep the code to a minimum, and certainly don't perform validation/processing/etc in your views.
Colin O'Dell
@yaya3: Oh, yes. I use Javascript, CSS and jQuery in my views all the time. But that's client-side code, not server side code.
Robert Harvey
@Robert Harvey yeh you are right - client side code is off topic here!
Dr. Frankenstein
+4  A: 

Generally speaking, the View should not contain any server-side business logic. But it can still contain logic that directly pertains to rendering the view.

An example would be a view containing some sort of variant record, whose display depends on the setting of a particular field. For example, a record that displays different information depending on a sex field being set to male or female. Which, of course, would require an if statement.

Robert Harvey
If you find yourself writing a lot of logic like nested ifs in the view, you should consider if your structural design is sound.
Robert Harvey
+4  A: 
Lèse majesté