in reply to Re: Problems getting info from .xml to display with HTML::Template
in thread Problems getting info from .xml to display with HTML::Template

XML now:
<document> <position> <departments> <department name = "Nothing Selected" /> <department name = "Pricing and Shopping Systems" /> <department name = "Commercial" /> <department name = "Operations Hardware" /> <department name = "Corporate Marketing" /> <department name = "Legal" /> <department name = "Human Resources" /> <department name = "Finance" /> </departments> </position> </document>
Perl now:
#!/perl use strict; use warnings; use lib 'C:/xampp/perl/site/lib'; use HTML::Template; $ENV{XML_SIMPLE_PREFERRED_PARSER} = 'XML::Parser'; use XML::Simple; use Data::Dumper; my $template = HTML::Template->new(filename => 'file'); my $xml = XMLin('file', ForceArray => ['departments']); my @department = @{ $xml->{position}->{departments} }; $template->param(DEPT => \@deptartment); print $template->output;
With strict on I get error: Global symbol "@department" requires explicit package name.

With strict off I get no errors, but nothing shows up on my template.

Using Dumper, my xml data looks right but displays something like

$VAR1=[ {department => { 'Finance' => {}, 'Human Resources' => {} } ]
I'm thinking maybe the empty curly braces have something to do with why it's not actually displaying anything?

Replies are listed 'Best First'.
Re^3: Problems getting info from .xml to display with HTML::Template
by moritz (Cardinal) on Jul 16, 2008 at 15:26 UTC
    my @department ..
    ...
    $template->param(DEPT => \@deptartment);
    ...

    With strict on I get error: Global symbol "@department" requires explicit package name.

    Do you notice anything? removing strict only hides the error.

    And I think you'll be more successful if you structure your XML to contain the data in the tags:

    <document> <position> <departments> <department>Nothing Selected</department> <department>Pricing and Shopping Systems</department> ... </departments> </position> </document>

    But then again I didn't test it.

      Jeez, can't believe I missed that one! Spelling mistake fixed and tags updated, but now... more problems!

      I'm getting this error:

      HTML::Template->output() : fatal error in loop output : HTML::Template +::param() : attempt to set parameter 'department' with an array ref - parameter +is not a T MPL_LOOP! at C:/xampp/perl/site/lib/HTML/Template.pm line 3068 at file.pl line 18
      My .tmpl contains:
      <font size="4">Department: </font> <ul><li><select name="dept"> <TMPL_LOOP DEPT> <option value="<TMPL_VAR DEPARTMENT>"><TMPL_VAR DEPARTMENT></optio +n> </TMPL_LOOP> </select></li> </ul>
      and again my perl is:
      my @department = @{ $xml->{position}->{departments} }; $template->param(DEPT => \@department); print $template->output();
      Aren't you supposed to set the name of <TMPL_LOOP> to an array?
        I don't know what your data structure now looks like, but this is how it should be for your template:
        my $department = [ { DEPARTMENT => 'Value1' }, { DEPARTMENT => 'Value2' }, { DEPARTMENT => 'Value3' }, ];

        If you don't get it from XML::Simple in that format, you have to loop over the array items to construct the hash references.