HTML::Template: Howto make a inner/outer loop

2004-04-05
2004-04-22
  • Eivind Hestnes
    Eivind Hestnes
    2004-04-05

    Hi,

    Short and to the point.

    1. I'm using this script to dump information from a database (and it works, yes :).

    dump.pl
    -----------------------------------------------------------------
    #!/usr/bin/perl

    use CGI qw/:standard :html3/;
    use CGI::Carp qw(fatalsToBrowser);
    use DBI;
    use DBD::mysql;

    my $DBName = "test";
    my $DBHost = "localhost";
    my $DBUser = "test";
    my $DBPass = "dev";
    my $DBType = "mysql";
    my $DBPort = "3306";

    sub db_connect {

        my ($result);

        $result = DBI->connect( "DBI:$DBType:database=$DBName;host=$DBHost",
        "$DBUser", "$DBPass", { 'RaiseError' => 0 } ) || &error("Unable to connect to database");

        return $result;

    }

    sub db_query {

        my ($result, $query);

        $query = $_[0];

        $result = &db_connect->prepare($query) || &error("Unable to query the database");
        $result->execute();

        return $result;

    }

    # outer loop
    my $query1 = &db_query("SELECT id, title FROM sections");
    while (my $rows1 = $query1->fetchrow_hashref()) {
        print "Section: " . $rows1->{title}."\n";

        my $sectionid = $rows1->{id};

            # inner loop
            my $query2 = &db_query("SELECT title FROM pages WHERE sectionid = $sectionid");
            while (my $rows2 = $query2->fetchrow_hashref()) {
                print "-> Page: " . $rows2->{title}."\n";
            }

    }
    -----------------------------------------------------------------

    2. Since I have started using HTML::Template on my webpage, I have to port this script to get use of a template.
    The template looks something like this:

    dump.html
    -----------------------------------------------------------------
    <TMPL_LOOP NAME="SECTIONS">
        Section: <TMPL_VAR NAME="TITLE"><br>
            <TMPL_LOOP NAME="PAGES">
                -> Page: <TMPL_VAR NAME="TITLE"><br>
            </TMPL_LOOP>
    </TMPL_LOOP>

    3. I have tried to make a new script for HTML::Template, but I can't get the script working with the template because of the outer/inner loop.

    Somebody out there with a solution? :-)

    Regards, Eivind Hestnes

     
    • steve
      steve
      2004-04-22

      Eivind,

      The result is of the form:

      my @sections = ()
      foreach my  $section_title (section_title_list) {
        my @pages = ()
        foreach my $page_title (page_title_list) {
            my $page_line = {
                title => $page_title
               };
            push @pages, $page_line;
        }
        my $section_info = {
                    title => $section_title,
                    pages => \@pages
               };
         push @sections, $section_info;
      }

      $template->param (sections => \@sections);

      I'm using code of this format right now.

      Good luck,
      Steve