pavani
pavani

Reputation: 143

error while using perl template toolkit

I am tried to get this data using template toolkit but I am unable to get.

$var1={
      'Object'=>[
                {
                 'Component'=>[
                               {
                                'type'=>'analog',
                                'name'=>'Temp',
                                'value'=>'23'
                                },
                                 {
                                'type'=>'digital',
                                'name'=>'Temp',
                                'value'=>'22'
                                },
                                 {
                                'type'=>'analog',
                                'name'=>'pressure',
                                'value'=>'23'
                                },
                                 {
                                'type'=>'analog',
                                'name'=>'humidity',
                                'value'=>'23'
                                }
                               ]
                             }
                            ],
                           };

I tried like this

[% FOREACH st IN Object %]
[% FOREACH st IN Object.Component %]
[% Component.type %][% Component.name %][% Component.value %]
[% END %]
[% END %]

I am not able to get the values and aslo it doesn't give any error ,please help how to get this values.

Upvotes: 0

Views: 379

Answers (2)

Nic Gibson
Nic Gibson

Reputation: 7143

You have two st variables. I don't remember exactly how TT handles scope but I suspect this not a good thing. You are also trying to extract values from a variable called Component. You haven't instantiated a variable called component. Remember that TT under normal circumstances doesn't give you an error if you reference a variable that doesn't exist, it simply outputs nothing.

If you want to change that so you get an error you need to set the STRICT configuration option. Alternatively, set one of the debug options:

my $template = Template->new(
    {
       STRICT => 1,      # or
       DEBUG => 'undef'
    });

Try something like:

[% FOREACH obj in Object %]
    [% FOREACH item in obj.Component %]
      [% obj.type %] [% obj.name %] [% obj.value %]
    [% END %]
[% END %]

Upvotes: 1

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38745

Change your template to:

[% FOREACH obj IN Object %]
[% FOREACH comp IN obj.Component %]
[% comp.type %] [% comp.name %] [% comp.value %]
[% END %]
[% END %]

Upvotes: 1

Related Questions