in reply to Re: Template Toolkit and Caching
in thread Template Toolkit and Caching

Ok, Templates first. Here's the main template being called, list.t:

[% WRAPPER 'pagebase.t' title="| Mark's Log" %] <div style="position:absolute;top:80px;left:10px;width:600px"> <h2>Mark's Log</h2> [% WRAPPER 'listbase.t' view='log' %] [% IF list.offset == 0 %] [% INCLUDE 'log/entry.t' log = entries.shift %] <div class="header">past log entries ...</div> [% END %] [% FOREACH log = entries %] [% r = text.crop(log.Body,200) log.Body = r.0 log.cropped = r.1 %] [% INCLUDE 'log/entry.t' %] [% END %] [% END %] </div> [% END %]

here is entry.t, which is called for each item in the entries array:

<h3> [% log.Subject %]<br /> <span class="date"> [% log.Date_Added %] </span> </h3> <p class="log"> [% text.nl2br(log.Body) %] [% IF log.cropped == 1 %] <span class="more" > [ <a href="details.cgi?view=log&id=[% log.ID %]"> more +...</a> ] </span> [% END %] </p> [% IF log.Date_Added != log.Date_Modified %] <p class="revision">last revised [% log.Date_Modified %]</p> [% END %]

here is listbase.t, a wrapper which handles displaying the list count and prev/next buttons:

<hr /><p class="more" >[% view %] [% list.offset + 1 %] - [% IF +list.offset + list.limit < list.total %][% list.offset + list.limit % +][% ELSE %][% list.total %][% END %] of [% list.total %]</p> [% content %] <hr /> <p class="more" > [% IF list.offset > 0 %] [ &lt; <a href="index.cgi?view=[% view %]&offset=[% list.off +set - list.limit %]"> previous [% list.limit %]</a> ] [% END %] [% IF list.offset + list.limit < list.total %] [ <a href="index.cgi?view=[% view %]&offset=[% list.offset + + list.limit %]">next [% list.limit %] </a>&gt; ] [% END %] </p>

and here is pagebase.t, a wrapper which controls the overall html page layout:

Content-Type: text/html; charset=UTF-8 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w +3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www/w3/org/TR/xhtml1" xml:lang="en-us"> <head> <title>Mark M. Tanny, Photographer [% title %]</title> <link rel="stylesheet" href="site.css" title="site wide style sh +eet" /> </head> <body> <div id="title" class="header" style="text-align:right;position: +absolute;top:30px;left:10px;width:600px;"> <a href="index.cgi?view=collections">Photo Collections</a> | <a href="index.cgi?view=log">Mark's Log</a> </div> [% content %] </body> </html>

and here's the script that calls list.t Still a little rough around the edges. Just so you know, $params{view} equals 'log' in this trial. A little rough around the edges still:

#------------------------------------- # PRAGMAS use lib 'e:/web_projects/marksphotos/lib'; use strict; use utf8; #------------------------------------- # IN HOUSE MODULES #use Config; use HTML::Tools; use dbWrapper qw( QueryDB ); #------------------------------------- # 3RD PARTY MODULES use Template; #===================================================================== +===== # MAIN #===================================================================== +===== my %params = ParseQueryString(); my $template = Template->new({INCLUDE_PATH => 'e:/web_projects/marksph +otos/templates/',DELIMITER=>';',CACHE_SIZE=>0}); my %db_info = (); my $page = ''; for( $params{view} ) { /log/ and do { $db_info{fields} = ['ID','Date_Added','Da +te_Modified','Subject','Body']; $db_info{table} = 'LogEntries'; $db_info{fieldnames} = $db_info{fields}; $db_info{count_table} = 'LogEntries'; $page = 'log/list.t'; last; }; /announcements/ and do { $db_info{fields} = ['ID','Date_Added','Da +te_Modified','Body']; $db_info{table} = 'Announcements'; $db_info{fieldnames} = $db_info{fields}; $db_info{count_table} = 'Announcements'; $page = 'announcements/list.t'; last; }; /collections/ and do { $db_info{fields} = ['C.ID','C.Date_Added' +,'C.Date_Modified','C.Title','C.Summary','P.Filename as Image']; $db_info{table} = 'PhotoCollections C LEF +T JOIN Photos P ON C.Feature_Image = P.ID'; $db_info{fieldnames} = [ 'ID','Date_Added +','Date_Modified','Title','Summary','Image' ]; $db_info{count_table} = 'PhotoCollections +'; $page = 'collections/list.t'; last; }; do{ print FrontPage(); exit(0); }; } #------------------------------------- my $list_limit = ( exists $params{limit} and $params{limit} =~ /^(\d ++)$/ ) ? $1 : 10; my $list_offset = ( exists $params{offset} and $params{offset} =~ /^(\ +d+)$/ ) ? $1 : 0; #------------------------------------- if( $db_info{table} ) { my $sth = undef; #-- sanity check for any available records ---------------------- { $sth = QueryDB("SELECT COUNT(ID) from ".$db_info{count_table}." WH +ERE Published='1'"); } my( $count ) = $sth->fetchrow_array; if( $count > 0 ) #-- there are records to fetch ---------------------------------- { if( $count > $list_offset ) #-- fetching a set of records in a valid range ----------------- +- { $sth = QueryDB("SELECT ".join(',',@{$db_info{fields}})." FRO +M ".$db_info{table}." WHERE Published='1' ORDER BY Date_Added DESC LI +MIT $list_offset,$list_limit"); my($arrayref,%hash) = ( undef ); push @{$arrayref},{ %hash } while( @hash{ @{$db_info{fieldna +mes}} } = $sth->fetchrow_array() ); #-- successfully retrieved records ------------------------- +----- if( scalar( @{$arrayref} ) ) { my $result = ''; $template->process($page,{entries=>$arrayref, list => { limit=>$list_limit, offset=>$list_offset +, total=>$count }, text => { crop => \&crop_text, nl2br => \&nl2br } }, \$result ) or die $template->error(); print $result,$count and exit(0); } else #-- couldn't retrieve records ------------------------------ +----- { print ErrorMessage("There has been an internal error") a +nd exit(0); } } else #-- tried to get set of records off the end of the table ------- +- { print ErrorMessage("You've tried to get a list of entries t +hat don't exist") and exit(0); } } else #-- there aren't any records to fetch --------------------------- { print ErrorMessage("There are no entries for display") and exit +(0); } } #===================================================================== +===== # SUBS #===================================================================== +===== sub FrontPage { return ErrorMessage("welcome to the front page"); } sub ErrorMessage { my $text = ''; $template->process('error.t',{message=>$_[0]},\$text ) or die $tem +plate->error(); return $text; } #===================================================================== +===== sub crop_text #a second return value of '1' indicates that the string was truncated. { my($text,$limit) = @_; $text =~ /^(.{1,$limit}\W)/s; return ($text,0) if( $text eq $1 ); return ($1,1); } # convert newlines to line breaks sub nl2br { my $text = $_[0]; $text =~ s|\n|<br />|g; return $text }