http://qs321.pair.com?node_id=11139384

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

Everlastingly Helpful Monks:

I have a module that contains a lot of constants for a web application. I also have many template toolkit templates that use varying sets of those constants. I'd like to include those constants in all my templates directly from my constants module rather than importing them to my script and then remembering to set the ones I need each time I process a template.

Here's an example of my constants module:

package Sam::Constants; use strict; use warnings; use base 'Exporter'; use constant PRE => 1; use constant REGULAR => 2; use constant POST => 3; our @EXPORT_OK = qw( PRE REGULAR POST ); 1;

I've fiddled with toolkit's PREPROCESS and STASH directives but got nowhere.

Thank you.

EDIT - here's an example of how I use the constants . . . .

---- list_txt.tt2 Here's a list for [% date %]: [% IF type == PRE %] PRE LIST [% list %] [% ELSIF type == REG %] REG LIST [% list_alt %] [% END %] [% INCLUDE email_footer_txt.tt2 %]

Here's roughly how the script currently uses the template . . .

use Sam::Constants qw(REG PRE POST); ... $content_ref->{PRE} = PRE; $content_ref->{REG} = REG; $content_ref->{list} = $list; $content_ref->{list_alt} = $list_alt; # for mailer and pass content variable to template my %email_args = ( from => $pool_ref->email, bcc => $pool_ref->admin->email1, text_template => 'list_txt.tt2', content_ref => $content_ref, ); require Template; my $tt = Template->new( { INCLUDE_PATH => $BASE_DIR . '/root/src/', INTERPOLATE => 1, } ) || die "$Template::ERROR\n"; $tt->process( $email_args{text_template}, $email_args{content_ref} ) || die $tt->error(), "\n";
I'd like to avoid having to put lines like these in my scripts:
$content_ref->{PRE} = PRE; $content_ref->{REG} = REG;