[Catalyst] TT custom virtual methods available to all pages.

Peter Karman peter at peknet.com
Wed Apr 23 16:06:25 BST 2008



On 04/23/2008 09:45 AM, Rajeev Cyrus wrote:
> Greetings.
> 
> One of my TT pages required some massaging of data that I decided to do
> via referencing a subroutine in a template variable, eg:
> $c->stash->{mk_hash} = \&mk_hash;
> 
> I would like to use this stashed variable on all of my templates so my
> question is:
> Where is the best place to initialize and set this variable? Where
> should i declare the mk_hash method? Is there some way I can configure
> catalyst to set certain template variables when the application starts?

I do that in MyApp::View::TT, as in this example:

http://search.cpan.org/src/KARMAN/Catalyst-Controller-Rose-0.04/t/examples/CatRose/lib/CatRose/View/TT.pm

I like to add virtual methods so that I can call them more easily. I prefer:

 [% mything.dump_data %]

to:

 [% USE Dumper; Dumper.dump_html( mything ); %]

Of course, you have to be careful when adding virtual methods, that you don't step on the
default ones. So I just have a couple I often use (dump_data and as_json).

-- 
Peter Karman  .  peter at peknet.com  .  http://peknet.com/




More information about the Catalyst mailing list