This thread makes me wonder if there are "Best Practices" guidelines for when and perhaps when not to create Plugin.
Early on, I thought in order to "extend" the Template Toolkit one had to write a plugin. I started writing some, but soon found that all I was doing was writing a wrapper that handled the extra $context argument that was prepended to the argument list. I dutifully dropped $context argument, called the new() method of the class I was wrapping and on I went. It worked, but it didn't seem like much of a plugin (hardly something I wanted to publish). Then upon re-reading the documentation I found the LOAD_PERL directive. I now use this for most of what I had been doing with thin plugin "wrappers". No extra code to write. LOAD_PERL seems like it does what I need the majority of the time, even though it's not enabled by default. It made me wonder then, and still does now...When is necessary to write a plugin vs. using LOAD_PERL. Is there a downside to using LOAD_PERL? I imagine the only time you really need to write a plugin is if you need access to the $context object. I went back to look at some of the stock plugins that come with TT2. Most of them seem to follow the "drop the $context variable on the floor" approach and then do something. The few that seem to use the $context argument seem to be setting up filters. What things have people found that the $context argument is required for the plugin? Anyone want to share when a Plugin is either the "only" way or the preferred way to go vs. using LOAD_PERL? Quoting darren chamberlain ([EMAIL PROTECTED]): > Bitton, David <[EMAIL PROTECTED]> said something to this effect on >02/05/2002: > > After Randal L. Schwartz's reply, I started to look at the > > Image::Size CPAN module. What's neat about that is its ability > > to return the height and width in formatted HTML, height="600" > > width="800", that I could just plop into my output. So I > > though I'd write an Template::Plugin::Image::Size. I'd like to > > see the Image::Info plugin, perhaps it'd save me some time. > > Thanks. > > I've used Image::Size in my templates before, but I put a > reference to imgsize into the params hash: > > use Image::Size (); > > my $t = Template->new; > my %params = ( > # some other stuff > imgfile => "/path/to/file", > imgsize => \&Image::Size::html_imgsize, > ); > > $t->process($file, \%params); > > And then in the template: > > <img src="[% imgfile %]" [% imgsize(imgfile) %]> > > Simple enough, and no nasty plugin business. :) > > (darren) >
