Bod has asked for the wisdom of the Perl Monks concerning the following question:
I have been following this tutorial for creating dynamic content with Template and trying to apply this to my own needs. But I've hit a problem in preparing the data for the template.
Template file
This does not generate any output for anything within the [% FOREACH ... %] block[% SET menuframe = ' menuselect' %] [% PROCESS admin_menu.tt %] [% FOREACH frame IN frames %] <hr> <p><b>[% frame.name %]</b><br> [% frame.colour %]</p> [% END %]
Here is the code that extracts some data from the database and prepares it for passing to the Template display code
There are a couple of warn statements in there so I can see something of what the code is doing.#!/usr/bin/perl use Site::Common; use Site::HTML; use strict; my $site = Site::Common->new; my $html = Site::HTML->new; my $dbh = $site->db; if ($data{'command'} eq 'frames') { my $vars = { 'frames' => \&list_frames, }; warn "Displaying template"; # This gets called $html->display("admin_frames", $vars); } else { $html->display("admin_pictures"); } sub list_frames { warn "Building list of frames"; # This is not called my @frames; my $query = $dbh->prepare("SELECT idFrame, name, colour FROM Frame +"); $query->execute(); while (my ($id, $name, $colour) = $query->fetchrow_array()) { my $frame = { 'id' => $id, 'name' => $name, 'colour' => $colour, }; push @frames, $frame; } return \@frames; }
Here is the relevant part of Site::HTML...
package Site::HTML; use Template; use Site::Variables; use strict; my $template = Template->new(INCLUDE_PATH => $Site::Variables::templat +e_path); sub display { my $self = shift; my $file = shift; my %vars = @_; $template->process("$file.tt", \%vars); } 1;
I'm not sure when list_frames is supposed to get called but I'm guessing at the time the hash reference $vars is generated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Preparing data for Template
by 1nickt (Canon) on Dec 30, 2020 at 23:50 UTC | |
by Bod (Parson) on Dec 31, 2020 at 00:21 UTC | |
by pryrt (Abbot) on Dec 31, 2020 at 00:51 UTC | |
by Bod (Parson) on Dec 31, 2020 at 11:05 UTC | |
by hippo (Archbishop) on Dec 31, 2020 at 11:47 UTC | |
| |
by GrandFather (Saint) on Jan 01, 2021 at 09:14 UTC | |
| |
by Bod (Parson) on Dec 31, 2020 at 11:17 UTC |