perlfect search + template::toolkit
"Martin Bonner" <[email protected]> Sat, 11 Oct 2003 19:59:01 +0100
| Newsgroups | gmane.comp.web.perlfect-search |
|---|---|
| Message-ID | <003a01c39029$bb907010$3301a8c0@ISOLDE> |
I was already making extensive use of Template::Toolkit for my site at the point of installing perlfect-search, so I've added the functionality to use Template::Toolkit for generating the output instead of Perlfect::Template On the off-chance that this functionality is of use to others, I've attached some patches (diffs are against search.pl version 1.97 but hopefully should apply okay to others) To enable this functionality, patch search.pl and add '$USE_TEMPLATE_TOOLKIT = 1;' to conf.pl. Setting this option to 0 will disable the TT functionality and revert to using Perlfect::Template. I've also written a short script for converting Perlfect::Template style templates to Template::Toolkit templates and have attached this also. Kind Regards, Martin.
perlfect2tt.pl
(application/octet-stream, 294 B)
#!/usr/bin/perl
use strict;
while (<>)
{
$_ =~ s/<\!--\s*cgi:\s*(\w*)\s*-->/\[% $1 %\]/g;
$_ =~ s/<\!--\s*loop:\s*results\s*-->/\[% FOREACH item = results %\]/g;
$_ =~ s/<\!--\s*item:\s*(\w*)\s*-->/\[% item\.$1 %\]/g;
$_ =~ s/<\!--\s*end:\s*results\s*-->/\[% END %\]/g;
print $_;
}
search.pl.diff
(application/octet-stream, 2.7 KB)
--- ../search-3.31b/search.pl 2003-05-22 00:15:34.000000000 +0100
+++ ./search.pl 2003-10-11 18:28:10.000000000 +0100
@@ -30,6 +30,10 @@
#use CGI::Carp qw(fatalsToBrowser);
use Fcntl;
use POSIX qw(strftime);
+if ($USE_TEMPLATE_TOOLKIT) {
+use Template;
+}
+
# added program path to @INC because it fails to find ./conf.pl if started from
# other directory
@@ -385,7 +389,18 @@
} else {
$file = $SEARCH_TEMPLATE{$lang};
}
- my $template = new Perlfect::Template($file);
+ my $template;
+ my $tt_output = '';
+ my $tt_vars;
+
+ if ($USE_TEMPLATE_TOOLKIT) {
+ $template = Template->new({
+ OUTPUT => \$tt_output,
+ ABSOLUTE => 1,
+ });
+ } else {
+ $template = new Perlfect::Template($file);
+ }
# %h carries values that will show up in the result page at <!--cgi: key-->:
$h{'script_name'} = "Perlfect Search $VERSION";
@@ -496,7 +511,20 @@
"&showurl=$show_url\">$HIGHLIGHT_TERMS{$lang}</a>)";
}
my $title = get_title_highlight($titles_db{$_}, $q);
- $template->cast_loop ("results", [{rank => $first+(++$rank),
+ if ($USE_TEMPLATE_TOOLKIT) {
+ push @{$tt_vars->{'results'}}, {
+ rank => $first+(++$rank),
+ url => $url,
+ highlight_link => $highlight_link,
+ visibleurl => $visible_url,
+ title => $title,
+ date => $date,
+ score => $score,
+ description => $desc,
+ size => sprintf("%.0f", $sizes_db{$_}/1000) || 1,
+ };
+ } else {
+ $template->cast_loop ("results", [{rank => $first+(++$rank),
url => $url,
highlight_link => $highlight_link,
visibleurl => $visible_url,
@@ -506,9 +534,10 @@
description => $desc,
size => sprintf("%.0f", $sizes_db{$_}/1000) || 1,
}]);
+ }
$result_count++;
}
- $template->finalize("results");
+ $template->finalize("results") unless ($USE_TEMPLATE_TOOLKIT);
$h{'results_num'} = $real_last;
my $last_page = ceil($real_last, $RESULTS_PER_PAGE);
@@ -562,8 +591,16 @@
$h{'search_time'} = sprintf(" in %.2f seconds", Time::HiRes::tv_interval($start_time));
}
- $template->cast(\%h);
- return $template->html;
+ if ($USE_TEMPLATE_TOOLKIT) {
+ foreach (keys %h) {
+ $tt_vars->{$_} = $h{$_};
+ }
+ $template->process($file,$tt_vars) || die "Template process failed: ", $template->error(), "\n";
+ return $tt_output;
+ }
+ else {
+ return $template->cast(\%h);
+ }
}
sub get_title_highlight {