tacogrande has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I almost have this but cannot figure out why its not showing the data, right now it knows how many entries im putting in because it spits out that number of rows. I am getting my data from dbd::mysql, it works because i have tested in in a normal test.pl and it prints out the data but not using an array or hash. I tried to follow this example: http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm#TMPL_LOOP I think its the second loop example on there. Here is my code on the perl side:
my @wall_data = (); while(my $wallref = $wallpostquery->fetchrow_hashref()) { my %walldata; $walldata{WALL_SUBJECT} = $wallref{'SUBJECT'}; $walldata{WALL_DATE} = $wallref{'DATE'}; $walldata{WALL_POSTID} = $wallref{'POSTID'}; $walldata{WALL_MESSAGE} = $wallref{'MESSAGE'}; push (@wall_data, \%walldata); ## i have also tried push @wall +_data, {%walldata}; } $template->param(WALL_LOOP => \@wall_data);
Web side:
<TMPL_LOOP NAME="WALL_LOOP"> <h3><a href="#"><TMPL_VAR NAME='WALL_SUBJECT'> - Posted by <TM +PL_VAR NAME="WALL_POSTID"> on <TMPL_VAR NAME="WALL_DATE"></a></h3> <div id="wallmessage"> <p><TMPL_VAR NAME="WALL_MESSAGE"></p> </div> </TMPL_LOOP>
thanks for those that help, sometimes handling data types is hard for me. -Scott

Node content and title restored by GrandFather

Replies are listed 'Best First'.
Re: html::template loops not showing data
by jbt (Chaplain) on Jul 11, 2009 at 03:10 UTC
    Your code on the server side is incorrectly using $wallref, try this:

    my @wall_data = (); while(my $wallref = $wallpostquery->fetchrow_hashref()) { my %walldata; $walldata{WALL_SUBJECT} = $wallref->{'SUBJECT'}; $walldata{WALL_DATE} = $wallref->{'DATE'}; $walldata{WALL_POSTID} = $wallref->{'POSTID'}; $walldata{WALL_MESSAGE} = $wallref->{'MESSAGE'}; push (@wall_data, \%walldata); ## i have also tried push @wall_dat +a, {%walldata}; } $template->param(WALL_LOOP => \@wall_data);
Re: html::template loops not showing data
by mzedeler (Pilgrim) on Jul 11, 2009 at 18:34 UTC

    Please consider not using HTML::Template in the first place. I strongly (update:) disagree with the underlying design philosophy and believe many other toolkits are better suited most tasks. For one, there is XML::Generator.

    It is much easier to iterate over lists as well as creating deep and complex HTML nodes. Let me know if you want an example.

      The main reason people use a templating system is to separate output formatting from the code. I fail to see how XML::Generator can provide something like that, as it only provides Perl code as the mechanism for creating the output, and no way of "fill out this template".

        Yes - XML::Generator doesn't separate code from the design, so if thats a high priority, its surely the wrong module to use.

        My main concerns with HTML::Template is the lacking support for deep structures. Its a very "flat" way of inserting values and doesn't really do much of the hard work, you'd expect. For one, traversing deep reference structures.

        Another big drawback is that you have no guarantee that you're generating valid XML (or xhtml) with HTML::Template.

        If designing separate view components (as in MVC) or something of the sorts, using XML::Generator to write smaller components is really straightforward and more reliable. You do lose the ability to use code-less templates, but in this era of xhtml and css, its not as important any longer.

      That is not a very persuasive argument :)