[Catalyst] C::M::{CDBI,DBIC} vs direct Loader use?

Brandon Black blblack at gmail.com
Thu Nov 17 03:35:11 CET 2005


On 11/16/05, Matt S Trout <dbix-class at trout.me.uk> wrote:
> On Wed, Nov 16, 2005 at 11:40:43AM -0500, Perrin Harkins wrote:
> > On Wed, 2005-11-16 at 06:59 -0600, Brandon Black wrote:
> > > I've started looking into that route now... since this localization
> > > should really happen generically to all DateTime objects being viewed
> > > through my View, I'm kinda thinking I should override
> > > Template::Stash's get() method to intercept (if ref  $_[0] eq
> > > 'DateTime') DateTime objects and change their timestamp before passing
> > > them on to the default get().  Or adopt a naming convention like I did
> > > with the database that all DateTime's to be localized end in "_stamp",
> > > and grep those variables out of the stash for post-processing in a
> > > subclass of Catalyst::View::TT before they get sent to Template:: at
> > > all.  Or something along those lines.
> >
> > Most people solve this sort of issue in the template by writing a TT
> > plugin (or even just a sub ref) that all the date display calls get
> > wrapped by.
>
> Yeah. Bear in mind that this works -
>
> $c->stash->{foo} = sub { "Foo to you, $_[0]"; };
>
> [% foo('Dr. Roberts'); %] # will add "Foo to you, Dr. Roberts" to the output
>

That's about the closest to clean now that I've seen so far that
actually works.  But I'll have to remember to wrap all of my date
variables used in templates ( [% timezone_ize(some_date_variable) %]
).  I'd still really like to just make it automagic for all DateTime
objects, but doing context-related magic on certain types of objects
before they are Viewed without using Singleton is proving to be quite
a pain.

The method of subclassing Catalyst::View::TT and pre-processing the
stash variables in a custom process() method before calling
NEXT->process() looked promising, but the issue there is that the
stash can be many arbitrary levels of nested objects, arrays, hashes,
etc, and I'd have to find a way to traverse them all in search of
DateTime objects, which would be both ugly and expensive.

The ultimate place to hook this in is in Template::Stash->get() I
think, where we can see through the various levels of indirection and
operate on a stash variable as it is used, but I haven't seen a way
(yet) to override that function cleanly with context available, and
honestly this is all a lot more trouble than just being unclean about
it and using a Catalyst::Plugin::Singleton context from my Model
during inflate().

-- Brandon



More information about the Catalyst mailing list