Straight from the reference manual for Template Toolkit, we see the following example for the IF directive:
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.[% 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 %]
In Catalyst code, using control logic to handle age:
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.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
So, to summarize.
- 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.
- 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 | |
Re: The fine line between control and display logic
by phaylon (Curate) on Jan 31, 2006 at 12:47 UTC | |
Re: The fine line between control and display logic
by LTjake (Prior) on Jan 31, 2006 at 12:47 UTC | |
Re: The fine line between control and display logic
by perrin (Chancellor) on Jan 31, 2006 at 15:55 UTC | |
Re: The fine line between control and display logic
by Anonymous Monk on Jan 31, 2006 at 12:27 UTC |
Back to
Meditations