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;

In reply to Import Constants From File to Templates? by varanasi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.