Hello Monks, I have been trying to use more of the MVC method for my web projects. To that end, I have be using the Template Toolkit a lot. On the this project I decided to use Class::DBI and Class::DBI::Pager. They work beautifully and I couldn’t be happier with them. The problem I am having is when I added Tie::Cycle to the mix. I pass the tied variable info my template, but only the first value is used. I’ve looked around a little and I haven’t seen any problem using ties in the template toolkit. Its probability something I am doing wrong, but I just don’t see it. Could someone point me in the right direction.

Thanks,

rlb3

PS. Code and Template found below.

#!/usr/bin/perl use strict; use warnings; use lib qw /./; use CGI; use Data; #Class::DBI use Template; use Tie::Cycle; my $q = CGI->new; my $file = 'index.html'; my $tt = Template->new({ INCLUDE_PATH => 'template/' }); tie my $cycle, 'Tie::Cycle', [ qw( FFFFFF CCCCCC ) ]; my $page = $q->param('page') || 1; my $pager = Data->pager(5,$page); my @data = $pager->retrieve_all; my $vars = { data => \@data, pager => $pager, cycle => $cycle }; print $q->header; $tt->process($file, $vars) || die "Template process failed: ", $tt->error(), "\n"; BEGIN TEMPLATE----------------------------------- <html> <head> <title></title> </head> <body> <table width="75%" border="0"> <tr> <th> &nbsp; </th> <th> Question 1 </th> <th> Question 2 </th> <th> Question 3 </th> <th> Date </th> </tr> [% FOREACH line = data %] <tr bgcolor="#[% cycle %]"> <td align="center"> [% loop.count %] </td> <td align="center"> [% line.one %] </td> <td align="center"> [% line.two %] </td> <td align="center"> [% line.three %] </td> <td align="center"> [% line.stamp %] </td> <td align="center"> <a href="delete.pl?id=[% line. +id %]">DELETE</a> </td> </tr> [% END %] </table> [% IF pager.previous_page %] <a href="index.pl?page=[% pager.previous_page %]">prev [% page +r.entries_per_page %]</a> | [% END %] [% IF pager.next_page %] <a href="index.pl?page=[% pager.next_page %]">next [% pager.e +ntries_per_page %]</a> [% END %] </body> </html>

In reply to Tied Variables in the Template Toolkit by rlb3

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.