http://www.perlmonks.org?node_id=1134506


in reply to Template::Toolkit - How do i access alias-based results?

To my way of thinking, SQL queries do not belong in templates.   I strictly think of templates as being presentation descriptions:   concerned with “how exactly to display” information which has been supplied to them, but not with the logic that supplies that information.   To me, and strictly IMHO, there is a “separation of concerns” issue here.

Template::Toolkit has many powerful features in it, which allow you to be very selective in exactly how the presentation is done.   But, to me, they should be used only for that purpose.   Procedural, active, logic does not belong in a template.

Replies are listed 'Best First'.
Re^2: Template::Toolkit - How do i access alias-based results?
by Yaerox (Scribe) on Jul 13, 2015 at 14:07 UTC
    I understand what you're saying but this doesn't help me on my issue right now. I already did it the other way around, receive first in perl and pass as param to template, but same result anyway...

    print "Content-type: text/html\n\n"; my $oTT = Template->new({ INCLUDE_PATH => '../templates', EVAL_PERL => 1, ABSOLUTE => 1, }) || die "$Template::ERROR\n"; # DB - Connect my $hDB = DBI->connect( "dbi:Oracle:xe", "lp", "lpdba", { AutoCommit = +> 0 } ); my $hStatement = $hDB->prepare(" SELECT v.vertragsnummer, a.artikelnr, v.status, v.datum, v.nachric +ht FROM vertraege v, artikel a, benutzer b, lieferanten l, mandanten +m [...]"); $hStatement->execute( ); while ( ( $iVertragsnummer, $sArtikelNr, $sVertragsStatus, $sVertragsD +atum, $iMsgID ) = $hStatement->fetchrow_array() ){ push (@aRows, ( $iVertragsnummer, $sArtikelNr, $sVertragsStatus, $s +VertragsDatum, $iMsgID )); } $hStatement->finish; $hDB->disconnect; my $sData = { 'result' => \@aRows }; $oTT->process("../templates/mytemp.html", $sData) || die $oTT->error() +, "\n";
      Using that code your @aRows will end up like this:
      row1-iVertragsnummer row1-sArtikelNr row1-sVertragsStatus row1-sVertragsDatum row1-iMsgID row2-iVertragsnummer row2-sArtikelNr row2-sVertragsStatus row2-sVertragsDatum row2-iMsgID ...etc
      I suspect you need something like this:
      { iVertragsnummer => 'row1-iVertragsnummer', sArtikelNr => 'row1-sArtikelNr', }, { iVertragsnummer => 'row2-iVertragsnummer', sArtikelNr => 'row2-sArtikelNr', },
      So try changing to:
      while ( my $row = $hStatement->fetchrow_hashref() ) { push ( @aRows, $row ); }
        Yeah this fixed it. I was trying around with fetchrow_arrayref but then I was in an endless loop. I should have better taken hasref ;)

        Anyway, thank you sir.

        Bingo.   I think that the data-structure is wrong.   Data::Dumper will quickly show you what the data actually looks like, and (as noted elsewhere) so will Template itself.

        What you need to come up with is an array of hashrefs, one per record, preferably the correct “slice” of a larger result set.   The template then loops through this array.