Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

The fine line between control and display logic

by santonegro (Scribe)
on Jan 31, 2006 at 10:53 UTC ( #526707=perlmeditation: print w/replies, xml ) Need Help??

Straight from the reference manual for Template Toolkit, we see the following example for the IF directive:
[% 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 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, but consider this more extensive example of a server database for Catalyst whose view is implemented in tt. Instead of two views, one for user and one for admin, we have a number of IF and UNLESS checks for admin interspersed in the code.

So, to summarize.

  1. moving "display logic" from templates to controllers can make HTML more readable. The extensive example above could be refactored into two pages, one for admin and one for users and therefore be more readable.
  2. moving "display logic" from templates to controllers insures scalability should additional model actions need to occur atomically with certain view actions.

Replies are listed 'Best First'.
Re: The fine line between control and display logic
by nothingmuch (Priest) on Jan 31, 2006 at 12:42 UTC
    I agree with Anonymous Monk - that's a technical demonstration, not a style guide...

    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.

    -nuffin
    zz zZ Z Z #!perl
Re: The fine line between control and display logic
by phaylon (Curate) on Jan 31, 2006 at 12:47 UTC
    I'd say that this is a rather bad example. And it seems it's used as an example for TT because it's just that, an example.

    In this (pretty rare) case I'd suggest having a login validation action, and grant/deny access with a specified message (Too young, much too young). This message can then be displayed in the login template itself. So view logic is a clean and easy [% login_message %], and the back set's the reason into $c->stash->{ login_message }.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: The fine line between control and display logic
by LTjake (Prior) on Jan 31, 2006 at 12:47 UTC

    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 she’ll be too busy laughing at you to even bother slapping you.
    " (src)

Re: The fine line between control and display logic
by perrin (Chancellor) on Jan 31, 2006 at 15:55 UTC
    I wouldn't send these to different templates, but I would move the comparison out of the template. I'd replace these age comparisons with booleans, and do the actual age checking in the controller.
Re: The fine line between control and display logic
by Anonymous Monk on Jan 31, 2006 at 12:27 UTC
    That TT example is not a demonstration of MVC.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://526707]
Approved by Mutant
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2019-12-09 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?