Hello the list,

I really love plugins, but I'm not fond of writing too simple plugin
like this one for the wonderfull DateTime :


                package Template::Plugin::DateTime;

                use strict;
                use base qw( DateTime Template::Plugin );
                sub new {
                        my $proto   = shift;
                        my $context = shift;
                        return $proto::SUPER->new(@_);
                }
                1;

                [% USE mydate = DateTime(year=> 2003, minute=> 2, hour=> 4) %]
                [% mydate.ymd('-') %]
                [% mydate.today.date %]
                           

Is there a way to avoid this job in vanilla TT ?
I've searched in archives without success.

One way to do the job could be to pass the package name as first
parameter.

                [% USE mydate=LazyLoad('DateTime', year=> 2004, minute=> 2, hour=> 4) 
%]
        
and the Template::Plugin::LazyLoad code :

                sub new {
                        my $class   = shift;
                        my $context = shift;
                        my $package = shift;
                        eval "require $package";
                        if ($@) {
                                        $class->error("couldn't load $package");
                                        return undef;
                        }
                        return $package->new(@_);
                }

It's a dirty "You'd better know what you're doing" hack... but it just
works (at least for the new constructor).
Should Template::Plugin::DateTime enter CPAN ?

Better anyone ?
                

Yann


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to