MVC - view files

True noob, here. I'm working on my 1st Coldbox project. (First MVC
framework, first non-procedural, etc.)

In Coldbox (or presumably other MVC frameworks) is it appropriate to
have any flow-control (e.g., <cfif>) tags in a view file? I.e., I
have a handler used for a form that has two versions, one a subset of
the other. So in the handler I set a variable in the request
collection and use it in the view file to conditionally display
certain things. Is that a no-no? I think of that as putting the
business logic in the handler (providing the appropriate value to the
variable), but I wonder....

Dan H

Flow control is perfectly fine in a View, as far as I'm concerned. You
are correct that the real logic, the deciding which form version to
show, should be done outside the view. In a rough break down, I'd
outline the MVC flow thusly:

Controller: grab the incoming data, figure out which Model to call and
ask for decisions from
Model: make the decisions based on data passed into it (in this case,
which form version to render)
Controller: take the decision made by the Model and figure out which
view(s) to invoke
View: use the data provided by the Controller to fill in the html

As you go along and find that some of the form parts you are using are
used elsewhere, you might want to break those out into their own files
so they can be easily included in multiple places. You don't have to
make the decision upfront though if you aren't ready, that's what
refactoring is for. As you go along coding, make a mental list of
where things seem to be being duplicated. When you run across one,
stop and think about how to refactor it out so that it isn't being
duplicated. Then continue on.

Hope that helps.

Cheers,
Judah

I think it depends on what it is, but let's say you are doing a form and you
have to do a condition on whether the person is under 13 for example. Then
you would have to do some form of conditional statements here.

Or let's say you are doing a pagination type navigation, and you want to do
a check whether to place the next / previous on the view.

My answer is it depends on what you are talking about, and what you are
trying to achieve. If it is pure business logic then yes I would be more
inclined to move it away from the view. Even if you do that you might still
need some conditional logic to see if that business rule/model is needed in
there or not as well.

Regards,
Andrew Scott
http://www.andyscott.id.au/

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of DH
Sent: Friday, 22 April 2011 6:27 AM
To: ColdBox Platform
Subject: [coldbox:9693] MVC - view files

True noob, here. I'm working on my 1st Coldbox project. (First MVC
framework, first non-procedural, etc.)

In Coldbox (or presumably other MVC frameworks) is it appropriate to have
any flow-control (e.g., <cfif>) tags in a view file? I.e., I have a

handler used

for a form that has two versions, one a subset of the other. So in the

handler

I set a variable in the request collection and use it in the view file to
conditionally display certain things. Is that a no-no? I think of that

as putting

It's fine for really dynamic views. It's better than repeating
yourself with several duplicate views. The rule of thumb that I
follow is that you should really avoid setting any variables in a
view. If you have a need to do that you should look and see if that
particular block of code should perhaps be in the handler instead.

Thanks, everyone!

Dan H