Re: data structure for Template Toolkit

2018-10-29 Thread Uri Guttman

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 

[% FOREACH course IN courses %]

[% courses.category %]


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.

  [% course.caption %] 


  L4 (HN)
               value="[% course.title %]_q1l4v[% courses.version %]"> 
1-4


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


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



Re: data structure for Template Toolkit

2018-10-29 Thread perl
Many thanks to Andrew and Mike — your suggestions worked and gave me a lot to 
think about as well!

That’s what love about this mailing list: I always learn a lot!

Rick Triplett


> 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";
> }

> On Oct 28, 2018, at 4:50 PM, Andrew Solomon  wrote:
> 
> Hi Rick,
> 
> The bug is that you're calling
> 
> my %list = (list => \@courses);
> 
> when you should be calling
> 
> my %list = (courses => \@courses);
> 
> If only there were 'strict' and 'warnings' for Template!  :-)
> 
> Andrew
> 
> On Sun, Oct 28, 2018 at 8:52 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 blanks.


Re: data structure for Template Toolkit

2018-10-29 Thread Mike Flannigan


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


On 10/28/2018 3: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.



start HTML 

[% FOREACH course IN courses %]

[% courses.category %]

  [% course.caption %] 


  L4 (HN)
               value="[% course.title %]_q1l4v[% courses.version %]"> 
1-4

               value="[% course.title %]_q2l4v[% course.version %]"> 
2-4

               value="[% course.title %]_q3l4v[% course.version %]"> 
3-4

               value="[% course.title %]_q4l4v[% course.version %]"> 
4-4



[% END %]