Ovid has asked for the wisdom of the Perl Monks concerning the following question:

Let's say I have the following Template Tookit template:

<html> <head><title>[% title %]</title></head> <body>[% INCLUDE "$site/$body" %]</body> </html>

If "$site/$body" is not found, I currently have a DEFAULT template set in the constructor:

my $template = Template->new( { INCLUDE_PATH => $some_include_path, DEFAULT => $some_default, PLUGIN_BASE => 'Onsite::Template::Plugin', OUTPUT => \$self->{_output} } );

The problem I have is that I want need to be automatically notified when a template fails to load (which is easy if I point the default template at a plugin), but I would like to know which template failed to load. In other words, with the above code, how would I pass the value of "$site/$body" to the $some_default template?

The only thing I've come up with is including the Data::Dumpered output of the Template object in the email, but that would be about as popular as meat-flavored ice cream.

And yes, this has already been posted to the Template mailing list :)

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Replies are listed 'Best First'.
•Re: Which template failed to load?
by merlyn (Sage) on Aug 13, 2002 at 04:31 UTC
    Don't use DEFAULT. I use something like this on Stonehenge's template setup:
    TRY; INSERT "stonehenge/insert/${template.class}/col${template.instance +}.pod" WRAPPER stonehenge/column_individual; CATCH file; "<!-- cannot find stonehenge/insert/${template.class}/col${templat +e.instance}.pod -->"; END;
    The TRY/CATCH lets me take an alternate path if something fails. And of course, I've got access to the very same variables in the alternate branch.

    -- Randal L. Schwartz, Perl hacker

Re: Which template failed to load?
by adrianh (Chancellor) on Aug 24, 2002 at 14:59 UTC

    One way of doing this would be to write a subclass of Template::Provider that records the location of the templates it fetches. For example:

    package Recording::Provider; use base qw(Template::Provider); my $Template_name; sub last_template { $Template_name }; sub fetch { my $self = shift; return( $self->SUPER::fetch($Template_name = shift) ); };

    You can then add this as the last provider in the chain - and the only one that handles the default template.

    my @config = ( INCLUDE_PATH => '/Users/adrianh/Desktop/template/', ); my $template = Template->new({ EVAL_PERL => 1, LOAD_TEMPLATES => [ Template::Provider->new(@config), Recording::Provider->new(@config, DEFAULT => 'default.t2'), ], }) or die "could not make template\n";

    Your default.t2 template can then do something like this to show what template failed to load:

    [% PERL %] print Recording::Provider->last_template [% END %] failed to load

    That last bit should probably more elegently implemented as a plugin - but I leave that as an exercise for the reader :-)

Re: Which template failed to load?
by BUU (Prior) on Aug 13, 2002 at 01:29 UTC
    the only way i can think of at the moment, Not being familiar with template toolkit, i can only offer what about passing it through a perl script? I.e. template=template.pl?myTemplate which could fetch the desired data, and check if it failed and so forth. The only other idea is to run some kind of periodic testing routine that calls a sucession of pages and parses the url to determine if it got the default..