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

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>

Replies are listed 'Best First'.
•Re: Tied Variables in the Template Toolkit
by merlyn (Sage) on Feb 24, 2004 at 03:37 UTC
      What do you mean by coderef? Could you please give me an example or point me to some resources, googling didn't really help.
        coderef => reference to a subroutine. Also known as a subref.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

      Merlyn,
      You are the MAN!!! Thanks so much..

      rlb3
Re: Tied Variables in the Template Toolkit
by perrin (Chancellor) on Feb 24, 2004 at 03:14 UTC
    The XS Stash doesn't support tied variables yet. You can use the normal (slower) Stash, or avoid tied variables (which are also slow, incidentally).