Perlfect Solutions
 

[Perlfect-search] perlfect search + template::toolkit

Martin Bonner perlfect-search@perlfect.com
Sat, 11 Oct 2003 19:59:01 +0100
This is a multi-part message in MIME format.

------=_NextPart_000_0037_01C39032.1D501D20
Content-Type: text/plain;
        charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

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.


------=_NextPart_000_0037_01C39032.1D501D20
Content-Type: application/octet-stream;
        name="perlfect2tt.pl"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
        filename="perlfect2tt.pl"

#!/usr/bin/perl=0A=
=0A=
use strict;=0A=
=0A=
while (<>)=0A=
{=0A=
  $_ =3D~ s/<\!--\s*cgi:\s*(\w*)\s*-->/\[% $1 %\]/g;=0A=
  $_ =3D~ s/<\!--\s*loop:\s*results\s*-->/\[% FOREACH item =3D results =
%\]/g;=0A=
  $_ =3D~ s/<\!--\s*item:\s*(\w*)\s*-->/\[% item\.$1 %\]/g;=0A=
  $_ =3D~ s/<\!--\s*end:\s*results\s*-->/\[% END %\]/g;=0A=
  print $_;=0A=
}=0A=

------=_NextPart_000_0037_01C39032.1D501D20
Content-Type: application/octet-stream;
        name="search.pl.diff"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
        filename="search.pl.diff"

--- ../search-3.31b/search.pl   2003-05-22 00:15:34.000000000 +0100=0A=
+++ ./search.pl 2003-10-11 18:28:10.000000000 +0100=0A=
@@ -30,6 +30,10 @@=0A=
 #use CGI::Carp qw(fatalsToBrowser);=0A=
 use Fcntl;=0A=
 use POSIX qw(strftime);=0A=
+if ($USE_TEMPLATE_TOOLKIT) {=0A=
+use Template;=0A=
+}=0A=
+=0A=
 =0A=
 # added program path to @INC because it fails to find ./conf.pl if =
started from=0A=
 # other directory=0A=
@@ -385,7 +389,18 @@=0A=
   } else {=0A=
     $file =3D $SEARCH_TEMPLATE{$lang};=0A=
   }=0A=
-  my $template =3D new Perlfect::Template($file);=0A=
+  my $template;=0A=
+  my $tt_output =3D '';=0A=
+  my $tt_vars;=0A=
+=0A=
+  if ($USE_TEMPLATE_TOOLKIT) {=0A=
+    $template =3D Template->new({=0A=
+                     OUTPUT =3D> \$tt_output,=0A=
+                     ABSOLUTE =3D> 1,=0A=
+    });=0A=
+  } else {=0A=
+    $template =3D new Perlfect::Template($file);=0A=
+  }=0A=
 =0A=
   # %h carries values that will show up in the result page at <!--cgi: =
key-->:=0A=
   $h{'script_name'} =3D "Perlfect Search $VERSION";=0A=
@@ -496,7 +511,20 @@=0A=
         "&amp;showurl=3D$show_url\">$HIGHLIGHT_TERMS{$lang}</a>)";=0A=
     }=0A=
     my $title =3D get_title_highlight($titles_db{$_}, $q);=0A=
-    $template->cast_loop ("results", [{rank =3D> $first+(++$rank), =0A=
+    if ($USE_TEMPLATE_TOOLKIT) {=0A=
+      push @{$tt_vars->{'results'}}, {=0A=
+                       rank =3D> $first+(++$rank),=0A=
+                       url =3D> $url,=0A=
+                       highlight_link =3D> $highlight_link,=0A=
+                       visibleurl =3D> $visible_url,=0A=
+                       title =3D> $title,=0A=
+                       date =3D> $date,=0A=
+                       score =3D> $score,=0A=
+                       description =3D> $desc,=0A=
+                       size =3D> sprintf("%.0f", $sizes_db{$_}/1000) || =
1,=0A=
+                       };=0A=
+    } else {=0A=
+      $template->cast_loop ("results", [{rank =3D> $first+(++$rank), =0A=
                        url =3D> $url,=0A=
                        highlight_link =3D> $highlight_link,=0A=
                        visibleurl =3D> $visible_url, =0A=
@@ -506,9 +534,10 @@=0A=
                        description =3D> $desc,=0A=
                        size =3D> sprintf("%.0f", $sizes_db{$_}/1000) || =
1,=0A=
                       }]);=0A=
+    }=0A=
     $result_count++;=0A=
   }=0A=
-  $template->finalize("results");=0A=
+  $template->finalize("results") unless ($USE_TEMPLATE_TOOLKIT);=0A=
   $h{'results_num'} =3D $real_last;=0A=
   =0A=
   my $last_page =3D ceil($real_last, $RESULTS_PER_PAGE);=0A=
@@ -562,8 +591,16 @@=0A=
     $h{'search_time'} =3D sprintf(" in %.2f seconds", =
Time::HiRes::tv_interval($start_time));=0A=
   }=0A=
   =0A=
-  $template->cast(\%h);=0A=
-  return $template->html;=0A=
+  if ($USE_TEMPLATE_TOOLKIT) {=0A=
+    foreach (keys %h) {=0A=
+      $tt_vars->{$_} =3D $h{$_};=0A=
+    }=0A=
+    $template->process($file,$tt_vars) || die "Template process failed: =
", $template->error(), "\n";=0A=
+    return $tt_output;=0A=
+  }=0A=
+  else {=0A=
+    return $template->cast(\%h);=0A=
+  }=0A=
 }=0A=
 =0A=
 sub get_title_highlight {=0A=

------=_NextPart_000_0037_01C39032.1D501D20--