Subject: | Using Config::Natural arrays to populate HTML::Template |
Date: | Mon, 16 Dec 2019 17:19:58 -0600 |
To: | bug-config-natural [...] rt.cpan.org |
From: | Bob McClure Jr <bob [...] bobcatos.com> |
Here is the platform:
Linux yak.bobcatos.com 2.6.32-754.24.3.el6.x86_64 #1 SMP Thu Nov 14 15:35:16 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi
Config::Natural v1.01
HTML::Template v2.97
I'm having trouble getting array data into my template. For this
config file fragment:
fruits = (
apple
banana
kiwi
orange
)
How do I interpolate that into the template:
<tmpl_loop name="fruits">
<tmpl_var name="fruit"> is a fruit.
</tmpl_loop>
Now if I use in my perl script:
my $config = new Config::Natural { auto_create_surrounding_list => 1 }, "test.cfg";
and my config file is:
fruit = apple
fruit = banana
fruit = kiwi
fruit = orange
Then I can in my perl script:
my $fruitref = $config->value_of("/fruits[*]");
my $tmpl = new HTML::Template
type => 'filename',
source => "test.tmpl",
die_on_bad_params => 0,
global_vars => 1,
associate => $config;
$tmpl->param('fruits' => $fruitref);
and the above template works. Howver, it breaks if there is only one
fruit because "fruits" is not defined. That's not surprising since
Config::Natural does some back-office finagling to create the fruits
array only when it sees multiple "fruit = ". Furthermore, some of my
data has 20 or 30 items in the array and putting that many
key = value1
key = value2
.
.
.
is a non-starter.
I have implemented an ugly kluge which requires embedding some HTML
into my Perl script, but it violates the principle of keeping those
things separate.
I really want to use the first config format without kluges. Is there
a way?
Cheers,
--
Bob McClure, Jr. Bobcat Open Systems, Inc.
bob@bobcatos.com http://www.bobcatos.com
Taste and see that the Lord is good; blessed is the one who takes
refuge in him. Psalm 34:8