in reply to Re: Import Constants From File to Templates?
in thread Import Constants From File to Templates?
Variation on the second approach you could load your values from an YAML file (or similarly JSON if that's your bag) rather than from code:
use Template; use YAML::XS qw( Load LoadFile ); use Cpanel::JSON::XS qw( decode_json ); ## This could be an external file, and/or make this a plugin argument +that points to it. my $consts = Load( <<'EOT' ); --- PRE: 1 REGULAR: 2 POST: 3 EOT ## Alternately my $alternate = decode_json( <<'EOT' ); {"PRE": 1, "REGULAR": 2, "POST": 3, "Foo": "Bar" } EOT my $template = Template->new({ CONSTANTS => { y => $consts, j => $alternate }, CONSTANTS_NAMESPACE => 'C', }); __END__ [% IF type == C.j.REGULAR %] Blah blah I R REGULAR stuff. But JSON says foo is '[% c.j.Foo %]' [% END %]
The cake is a lie.
The cake is a lie.
The cake is a lie.
|
|---|