Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Import Constants From File to Templates? (outside-in) (updated)

by LanX (Saint)
on Dec 04, 2021 at 18:50 UTC ( [id://11139392]=note: print w/replies, xml ) Need Help??


in reply to Import Constants From File to Templates?

> I'd like to include those constants in all my templates directly from my constants module rather than importing them to my script

OK if you don't need those constants at all ...

Instead of passing the exported constants each time with ->process

>

my $tt = Template->new( { INCLUDE_PATH => $BASE_DIR . '/root/src/', INTERPOLATE => 1, } ) || die "$Template::ERROR\n"; $tt->process( $email_args{text_template},

you could make your config module export the initialized TT object

package Sam::Constants; use Template; our $tt = Template->new( # export me { INCLUDE_PATH => $BASE_DIR . '/root/src/', INTERPOLATE => 1, CONSTANTS => { ... }, VARIABLES => { ... }, } ) || die "$Template::ERROR\n";

(see also Re: Import Constants From File to Templates?)

And if things like $BASE_DIR need to be flexible, provide an import() function.

use Sam::Constants base_dir =>  "...";

edit

and if you have to manage different constructor setups, export functions which do the different construction and return the specialized $tt object.

update

package Sam::Constants; my @defaults_all = (... ); sub foo { return Template->new( @_ , @defaults_all, DEFAULTS_foo ) }; sub bar { return Template->new( @_ , @defaults_all, DEFAULTS_bar ) }; ... # other file use Sam::Constants qw/foo/; my $tt = foo(LOCAL_CFG); $tt->process();

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139392]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found