[Catalyst] Re: template design issue: varibales stand-alone components

Aristotle Pagaltzis pagaltzis at gmx.de
Mon Feb 23 10:05:54 GMT 2009


* Bill Moseley <moseley at hank.org> [2009-02-22 18:20]:
> On Sun, Feb 15, 2009 at 11:05:20AM +0100, Aristotle Pagaltzis wrote:
> > Your templates should build up the page inside-out, and they
> > should not be calling *any* methods in your model. As far as
> > possible they should only be using stashed variable values.
> > If you call any methods they should only be helper methods
> > from the context like `uri_for`.
>
> Hope that quote is not out of context.
>
> So are you saying you should never do this?:
>
>     Hello [% user.first_name | html %]
>
> That instead you should have in your controller:
>
>     $c->stash->{first_name} = $user->first_name;

Ack, no. What I said really didn’t make much sense, although what
I actually meant does. Now that you pointed out the inconsistency
I’m not sure how to put my point as a simple rule of thumb.

No, `user.first_name` in the template is perfectly acceptable.
Calling methods to retrieve values from stashed objects is
perfectly reasonable.

What I meant is that the templates should not go fetching data
from the the model by themselves, nor of course should they be
making insert/delete calls against the model. They should also
avoid calling any methods on the context object, as far as
possible. Frequent exceptions are `c.uri_for` and `c.req`’s
various methods.

The point is: if rendering the template requires the entire Cat
app to be set up and running, you are probably relying on global
state, and the template will be hard to test in isolation and
hard to reuse. Ideally it would be no harder to render from a
10-liner than from the Cat app.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list