in reply to Import Constants From File to Templates?
In your current implementation you could (ab)use the EXPORTS_OK list to import the constants as template variables:
my $content_ref = { map { $_ => Sam::Constants->$_ } @Sam::Constants::EXPORT_OK }You could also pre-load them into the configuration of your >TT object. Here's a complete example:
When run, this prints:use strict; use warnings; package Sam::Constants; use base 'Exporter'; use constant PRE => 1; use constant REGULAR => 2; use constant POST => 3; our @EXPORT_OK = qw( PRE REGULAR POST ); package main; use Template; my $text = <<EOT; The constant 'PRE' has the value [% PRE %] The constant 'REGULAR' has the value [% REGULAR %] The constant 'POST' has the value [% POST %] EOT my $tt = Template->new( { VARIABLES => { map { $_ => Sam::Constants->$_ } @Sam::Constants::EXPORT_OK }, } ) || die "$Template::ERROR\n"; $tt->process( \$text ) || die $tt->error(), "\n";
The constant 'PRE' has the value 1 The constant 'REGULAR' has the value 2 The constant 'POST' has the value 3
|
|---|