Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

data structure for Template Toolkit

32 views
Skip to first unread message

Rick T

unread,
Oct 28, 2018, 5:00:04 PM10/28/18
to Perl Beginners
As a novice in perl I realize that it’s a bit presumptuous for me to attempt references and complex data structures. But I had a need and gave it a shot — a failing shot. I’ve been fiddling with my failure, almost mindlessly, all weekend; now I need some help.

Below is the template segment I am trying to populate with data, and following it is the segment of code that attempts to call it. The output I get in my browser is persistently empty, with every instance of [% %] being replaced with banks.


start HTML ------------
<table>
[% FOREACH course IN courses %]
<br><br>
<h2 id="[% course.cat_abbr %]">[% courses.category %]</h2><br><br>

<table> <caption> [% course.caption %] </caption>
<!-- LEVEL 4 -->
<tr style="background-color: Plum">
  <td>L4 (HN)</td>
  <td><input type="checkbox" name="course_file" 
             value="[% course.title %]_q1l4v[% courses.version %]"> 1-4</td>
  <td><input type="checkbox"  name="course_file" 
             value="[% course.title %]_q2l4v[% course.version %]"> 2-4</td>
  <td><input type="checkbox" name="course_file" 
             value="[% course.title %]_q3l4v[% course.version %]"> 3-4</td>
  <td><input type="checkbox" name="course_file" 
             value="[% course.title %]_q4l4v[% course.version %]"> 4-4</td>

</table>
[% END %]
<!-- end of Template loop —>


end HTML ————————————— start perl:

my ( $cat_abbr, $category, $title, $caption, $version ); # for Template
my ( @courses, $string ); # for calculating
while ( my $line = <DATA> ) {
    ( $cat_abbr, $category, $title, $version ) = split /\t/, $line;
    chomp $version;
    $caption = $title;
        $caption =~ s{_|\-}{ }xmsg;     # replace _ and - with space
        $caption =~ s{\b(\w)}{\U$1}g;   # impose simple titlecase
        $caption = "$caption" . ' (version ' . "$version" . ')';

    push @courses,
        {
            cat_abbr => $cat_abbr,
            category => $category,
            caption  => $caption,
            title    => $title,
            version  => $version,
        }
}

my %list = (list => \@courses);

# Call Template Toolkit
local $| = 1; # auto flush buffer
my $path = "/big/dom/x$server/www/templates";
my $tt = Template->new(
    {INCLUDE_PATH => "$path"}
);
my $input = 'course_catalog.tt';
my $vars = \%list;
print "Content-Type: text/html\n\n";
$tt->process($input, $vars) 
    or die $tt->error();

exit;

# tab separated lines of trial data
__DATA__
eng ENGLISH english 2
american-literature 1
english-literature 1
sci SCIENCE integrated-science 2
biology 2
chemistry 1
physics 1
soc SOCIAL STUDIES geography-and-cultures 1
world-history 2
american-history 1
ele ELECTIVES health 1


Any and all advice will be most welcome!
Rick Triplett

Mike Flannigan

unread,
Oct 29, 2018, 8:00:04 AM10/29/18
to begi...@perl.org, pe...@reason.net

I hope somebody has replied already.
If not, in general you are reading data from DATA,
creating an array and a hash, and then creating an
html file using the Template Toolkit.

I think much of it looks good, but I see no
use Template::Toolkit
or anything similar.
Do you have that?  I suspect you do.
Maybe you should post that part too.


Also, this line looks suspicious to me:

my %list = (list => \@courses);

Maybe that is intended to be an array ref.

Perhaps right after that you should put this:

print "\%list contains this:\n\n";
foreach my $key (sort keys %list){
    print "$key - $
list{$key}\n";
}

__END__



This is a way for you to debug it yourself.
If that works as expected, then get back to us.


Mike

Uri Guttman

unread,
Oct 29, 2018, 5:00:05 PM10/29/18
to begi...@perl.org
On 10/28/18 4:45 PM, Rick T wrote:
As a novice in perl I realize that it’s a bit presumptuous for me to attempt references and complex data structures. But I had a need and gave it a shot — a failing shot. I’ve been fiddling with my failure, almost mindlessly, all weekend; now I need some help.

Below is the template segment I am trying to populate with data, and following it is the segment of code that attempts to call it. The output I get in my browser is persistently empty, with every instance of [% %] being replaced with banks.



i have many small comments as well as a larger idea that you try Template::Simple instead. it allows for arrays (such as you have) of hashes to be used without any looping code in the template itself. in fact it doesn't support any logic in the template as that should always been in perl IMNSHO. it is cleaner, faster and more flexible to organize data in perl than to do it in a template.

start HTML ------------
<table>
[% FOREACH course IN courses %]
<br><br>
<h2 id="[% course.cat_abbr %]">[% courses.category %]</h2><br><br>

i was confused by the courses.category there. it should be course.category as courses has only hashes and no values in it.

i don't see how you are making a table for each category. it seems to be one for each data row. but that is a different problem. nut i see that these are all checkboxes so that make work.
<table> <caption> [% course.caption %] </caption>
<!-- LEVEL 4 -->
<tr style="background-color: Plum">
  <td>L4 (HN)</td>
  <td><input type="checkbox" name="course_file" 
             value="[% course.title %]_q1l4v[% courses.version %]"> 1-4</td> 

my ( $cat_abbr, $category, $title, $caption, $version ); # for Template
my ( @courses, $string ); # for calculating
while ( my $line = <DATA> ) {

do your chomp here as it is clearer to chomp the line instead of the last field on the line.

    ( $cat_abbr, $category, $title, $version ) = split /\t/, $line;

declare those variables there and not earlier. they are only used inside the loop.

    chomp $version;
    $caption = $title;
        $caption =~ s{_|\-}{ }xmsg;     # replace _ and - with space
use // for regexes without any use of / inside. also that regex is better as a character class [_-]. the - doesn't need to be escaped in a regex but only inside a char class. and if - is the first or last char in a char class (as i showed) it doesn't need escaping.

and even better, replacing single chars is best done with the tr/// op. that would be tr/-_/ /

        $caption =~ s{\b(\w)}{\U$1}g;   # impose simple titlecase
        $caption = "$caption" . ' (version ' . "$version" . ')';
better as:

            $caption .= "(version $version)" ;

    push @courses,
        {
            cat_abbr => $cat_abbr,
            category => $category,
            caption  => $caption,
            title    => $title,
            version  => $version,
        }
you didn't end that statement with a ;. it is legal as ; are really statement separators and that is the last statement in the loop block. still it is good style to always end statements with ;


}

my %list = (list => \@courses);

# Call Template Toolkit
local $| = 1; # auto flush buffer
my $path = "/big/dom/x$server/www/templates";
my $tt = Template->new(
    {INCLUDE_PATH => "$path"}

don't quote single variables like that. it isn't needed, it is slower due to an extra copy and it could be a bug if you pass a reference that gets converted to a string.
);
my $input = 'course_catalog.tt';
my $vars = \%list;
you don't need $vars. you can just pass \%list to the call.

print "Content-Type: text/html\n\n";
$tt->process($input, $vars) 
    or die $tt->error();


if i have the spare time, i will post how to do this with template::simple.

uri

Rick T

unread,
Oct 30, 2018, 8:45:06 AM10/30/18
to Uri Guttman
Uri, thanks for the boatload of useful suggestions! I will am busying myself with understanding and applying them.

Regarding Template::Simple, this module isn’t among the many that my host has installed, and I have not had success trying to learn how to install stuff from CPAN. Fortunately I’m beginning to get the hang of Template Toolkit, and I may need it for more complex situations that come up. Switching from HTML::template this year was a challenge, and I had to rewrite a lot of my perl to go with it. I know that most perl people are good at using the CPAN resource, but I’m just a high school teacher who dabbles in perl. And at age 74 I have to be choosy about which learning curves I take on!

Still, if anyone can point me to a “beginners guide” to using CPAN, I’ll take a look at it.

Rick Triplett

pe...@reason.net

unread,
Oct 30, 2018, 2:00:05 PM10/30/18
to Perl Beginners
Looks like a good place to start. When I get the courage, I’ll plunge in! — Rick


On Oct 30, 2018, at 12:40 PM, Shlomi Fish <shl...@shlomifish.org> wrote:

Hi Rick,


On Tue, 30 Oct 2018 07:34:11 -0500
Rick T <pe...@reason.net> wrote:

Uri, thanks for the boatload of useful suggestions! I will am busying myself
with understanding and applying them.

Regarding Template::Simple, this module isn’t among the many that my host has
installed, and I have not had success trying to learn how to install stuff
from CPAN. Fortunately I’m beginning to get the hang of Template Toolkit, and
I may need it for more complex situations that come up. Switching from
HTML::template this year was a challenge, and I had to rewrite a lot of my
perl to go with it. I know that most perl people are good at using the CPAN
resource, but I’m just a high school teacher who dabbles in perl. And at age
74 I have to be choosy about which learning curves I take on!

Still, if anyone can point me to a “beginners guide” to using CPAN, I’ll take
a look at it.


See https://perl-begin.org/topics/cpan/ for some guides for using CPAN.

Regards,

Shlomi Fish

pe...@reason.net

unread,
Oct 30, 2018, 3:00:05 PM10/30/18
to Perl Beginners
Thanks, Brandon! I’m hosted at FutureQuest.net, which has Linux servers. So, nothing oddball or demanding about the environment. But my familiarty with Unix/Linux programming is almost nil, so I sweat over stuff that would seem clear to most programmers. — Rick


On Oct 30, 2018, at 1:41 PM, Brandon McCaig <bamc...@gmail.com> wrote:

On Tue, Oct 30, 2018 at 07:34:11AM -0500, Rick T wrote:
Still, if anyone can point me to a “beginners guide” to using
CPAN, I’ll take a look at it.

The easy button for CPAN, or at least the one I'm most familiar
with, is cpanm AKA App::cpanminus. The cpan command is very
low-level and prompts the user repeatedly, making it basically
unusable by a human user. If it is available to you, or you're
able to get it installed, cpanm takes away all of this pain and
automatically fetches, builds, tests, and installs your desired
package and all of its dependencies automatically.. You'd just
invoke:

   $ cpanm Template::Simple

And with minimal output it should install for you. If not, you
might have to get your hands dirty reading logs to figure out
what is wrong, and what to do about it, but for well maintained
packages in a Unix-like environment that is rare in my
experience..

You can read about cpanm and how to install it from the Git repo
readme:
https://github.com/miyagawa/cpanminus/tree/devel/App-cpanminus

I have not used hosting environments for Perl deployment so I
cannot say whether this is likely to be an option for you.

On another note, a nice way to control your Perl environment is
by using perlbrew, which allows you to install an up-to-date perl
distribution within your home directory to use, and to likewise
install your CPAN modules within your home directory too. It is
also capable of managing many different perl environments
side-by-side which probably isn't needed, but doesn't hurt to
know about..

I'm not sure how difficult it would be to use a perlbrew
environment for your hosting environment, but nevertheless it is
a useful resource to know about. Note that if you do use perlbrew
it has a command to install cpanm too so it might kill two birds
with one stone.

Regards,


--
Brandon McCaig <bamc...@gmail.com> <bam...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'


Mike Flannigan

unread,
Oct 31, 2018, 7:30:04 AM10/31/18
to begi...@perl.org

Are you on Linux or using Strawberry Perl?
I used Activestate for 15+ years and I was surprised
by how easy it was to move to Strawberry Perl about
3 years ago.  It's pretty darn easy to install modules
with Strawberry Perl and seems to work every time.


Mike


Uri, thanks for the boatload of useful suggestions! I will am busying myself with understanding and applying them.

Regarding Template::Simple, this module isn’t among the many that my host has installed, and I have not had success trying to learn how to install stuff from CPAN. Fortunately I’m beginning to get the hang of Template Toolkit, and I may need it for more complex situations that come up. Switching from HTML::template this year was a challenge, and I had to rewrite a lot of my perl to go with it. I know that most perl people are good at using the CPAN resource, but I’m just a high school teacher who dabbles in perl. And at age 74 I have to be choosy about which learning curves I take on!

Still, if anyone can point me to a “beginners guide” to using CPAN, I’ll take a look at it.

Rick Triplett

Rick T

unread,
Oct 31, 2018, 12:45:04 PM10/31/18
to Perl Beginners
Linux
0 new messages