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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.