[% IF age < 10 %]
Hello [% name %], does your mother know you're
using her AOL account?
[% ELSIF age < 18 %]
Sorry, you're not old enough to enter
(and too dumb to lie about your age)
[% ELSE %]
Welcome [% name %].
[% END %]
Here we see variable output based on age and we see the use of display logic to handle it. But, a different way to think about solving this problem is to have a controller dispatch to 3 separate sub-actions, one for each age bracket, each of which points to a different view. Why would one go to this trouble? Because it scales to possible model actions later: logging each visit based on age, for example.
In [cpan://Catalyst] code, using control logic to handle age:
package MyApp::C::Login;
sub login : Path('/login') {
my ( $self, $c ) = @_;
my $age = $c->req->params->{age} ;
if ($age < 10) {
$c->forward('under10');
} elsif ($age < 18) {
$c->forward('under18');
} else {
$c->forward('welcome');
}
sub under10 : Private {
my ( $self, $c ) = @_;
$c->model('Login')->incr_under10;
$c->stash->{template} = 'under10.tt';
}
# and so forth
Now this might seem like overkill
I would probably be inclined to have under10 and under18 in the same page, since they're probably similar, but keep welcome separate.
When too much display logic is in your controller your concerns are also mixed up - the controllers that handle logic (deciding on age) should not be concerned with the specific message that the page is displaying. It should be concerned with differences that are fundamentally different in their behavior. At least IMHO.
You could probably argue back and forth ad nauseam as to how much or how little logic should go in the view. To me, the important thing is to strike a balance that results in a maintainable structure.
There are no definitive answers -- for which I'm glad, since it allows me/my workplace to set our own guidelines and create our own desirable structure.
--
"Go up to the next female stranger you see and tell her that her "body is a wonderland."
My hypothesis is that shell be too busy laughing at you to even bother slapping you." (src)
perlmonks.org content © perlmonks.org and Anonymous Monk, LTjake, nothingmuch, perrin, phaylon, santonegro
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03