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

Hello everyone

I have recently incorporated Class::DBI into my CGI::Application (whichi is wicked cool just incase anyone had any doubts) and I wanted to exploite it a little more. My problem is that I use HTML::Template and right now don't want to swtich to Template::Toolkit. I found the plugin for HTML::Template that allows for Dot magick and that seems to help me for simple things.

But for the life of me I cannot figure out how to use simple loops. For instance this works:

# In perl script my $article = MPDatabase::Article->retrieve($articleId); $template->param(article => $article); return $template->output; # In the template file <!-- TMPL_VAR NAME="article.title" --> <!-- TMPL_VAR NAME="article.author" --> etc...

But this does not:

# In perl script my @articles = MPDatabase::Article->retrieve_all; $template->param(article_loop => \@articles); return $template->output; # In the template file <!-- TMPL_LOOP NAME="article_loop" --> <!-- TMPL_VAR NAME="this.title" --> <!-- TMPL_VAR NAME="this.author" --> etc... <!-- /TMPL_LOOP -->

By looking at the example the docs give I would assume I could do this. In the docs they only give an example where the array is an element of the variable they pass in. They do not show how to do it if you are passing in the array directly. Can I just not do this? Thanks

!!UPDATE!!

Okay so I sorta figured it out. If I do this:

foreach my $article ( @articles ) { print STDERR $article->anyColumnName; }

before I populate the template params it works. I think this has to do with the lazy loading thing of Class::DBI? Now what to do about it.... hrm...

Replies are listed 'Best First'.
Re: HTML::Tempate::Dot
by rhesa (Vicar) on Jun 09, 2006 at 11:46 UTC
    Hi debiandude,

    I wrote HTML::Template::Plugin::Dot, and I'm pretty sure this ought to work. See the t/dot_loop.t test in the distribution. I'll paste it here verbatim:

    As you can see here, I'm passing in an arrayref directly. Which version of HTML::Template do you have? What does this test result in for you?

    We use HT::Dot extensively in our company, in combination with CGI::Application and Class::DBI. It works okay, but I'd be the first to admit that it can't compete with the Template Toolkit. HT::Dot is limited in what it can accomplish, mainly because of the way HTML::Template itself is structured. It was easy to put some form of dot-expression parsing inside param(), but it would be very difficult to actually hook it into H::T's parsing code. If I had to muck around in there, I'd be creating a real fork of HTML::Template. And that would be futile, since TT2 is already so much better.

    Rhesa

      Hey rhesa,

      Thanks for the quick response. I ran the test and all five of them passed. I am thinking maybe it might be something to do with the fact that I am using HTML::Template::Plugin::Dot though CGI::Application::Plugin::HTDot? I am going to fire up CPAN and check to see if I have the latest versions. If not I will update and see if it works then.

Re: Using loops with HTML::Template::Dot
by strat (Canon) on Jun 10, 2006 at 10:39 UTC

    I think HTML::Template::Compiled offers this functionality, and until now all my HTML::Template's run under HTML::Template::Compiled as well.

    Best regards,
    perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.11.32"