Skip to main content.
home | support | download

Back to List Archive

cgi question/template questions

From: Aaron Bazar <yeranb(at)>
Date: Wed Jun 25 2003 - 16:11:07 GMT
Hi Everybody!

If this question is a repeat, I apologize. It seems I am having some trouble
sending email to the list.

I have a small problem. I would like to add a bit of functionality to the
cgi script, but I am not quite sure where to do it.

I would like to take the query string that somebody enters during a search,
and use that string to generate a listing of related keywords, and then
print that out in the results. However, with the use of the templates, etc,
I can't quite figure out how to go about it (my knowledge of object oriented
perl is also VERY limited).

Right now, I have a piece of script that generates a random set of keywords
from list that I have. I put this small snippet of code right in the "" file, below the "ugly hack" line. The list of
keywords is pulled from the file, and set to a variable, that I have added
to the list of $params. ( MENU        => $menuInfo) ... So, in the
swish.tmpl file, I have a template variable called MENU that the list gets
put into automatically. It works fine, even if it is a really ugly hack, but
I can't figure out how to get query_simple into the program...

In the list of $params, I see "QUERY_SIMPLE    => CGI::escapeHTML(

How can I make that variable or data available at the top of the program so
I can use it to make a RELATED keywords tag, (or replace the MENU tag, I
have already created). I suspect this is an easy question for somebody who
understands OO perl... I need to avoid the errors I have gotten before,
though... variable XXX needs explicit package name, etc. etc...

Anyway, here is my code... It ain't pretty, I know...


bash-2.05$ more
#  Module for using Template-Toolkit for generating output
#    $Id:,v 1.1 2001/12/08 21:49:14 whmoseley Exp $
#  This module probably does not automatically support all the features
#  of the swish.cgi script (such as selecting index files).  See
#  the module for examples.
package TemplateHTMLTemplate;
use strict;

use HTML::Template;
use HTML::FillInForm;
use CGI ();

use vars '$Template';

use TemplateDefault;  # ugly hack
my $menuInfo;
open(FILE1,"/home/virtual/") ||
die("cannot open file ");
my @phraseList =<FILE1>;

my $phrase1 = $phraseList[rand(@phraseList)];
my $phrase2 = $phraseList[rand(@phraseList)];
my $phrase3 = $phraseList[rand(@phraseList)];
my $phrase4 = $phraseList[rand(@phraseList)];
my $phrase5 = $phraseList[rand(@phraseList)];
my $urlPhrase1 = $phrase1;
my $urlPhrase2 = $phrase2;
my $urlPhrase3 = $phrase3;
my $urlPhrase4 = $phrase4;
my $urlPhrase5 = $phrase5;
$menuInfo = "<table width=\"99\%\" border=\"0\"><tr>    <td><div
align=\"center\"><font size=\"-1\" face=\"Geneva, Arial, Helv
etica, San-Serif\"><b>Popular Searches</b></div>
    </td></tr><tr><td><ul><font size=\"-1\" face=\"Geneva, Arial, Helvetica,
San-Serif\"><li><a href=\"/health/$phrase1\">"."$
urlPhrase1"."</a>" .
"<li><a href=\"/health/$phrase2\">"."$urlPhrase2" ."</a>" .
"<li><a href=\"/health/$phrase3\">"."$urlPhrase3". "</a>" .
"<li><a href=\"/health/$phrase4\">"."$urlPhrase4" . "</a>" .
"<li><a href=\"/health/$phrase5\">"."$urlPhrase5". "</a>"."</ul>    </td>

sub show_template {
    my ( $class, $template_params, $results ) = @_;

    my $cgi = $results->CGI;

    my $template = HTML::Template->new( %{$template_params->{options}} );

    my $params = {
        TITLE           => ($results->config('title') || 'Search Page'),
        QUERY_SIMPLE    => CGI::escapeHTML( $results->{query_simple} ),
        MESSAGE         => CGI::escapeHTML( $results->errstr ),
        QUERY_HREF      => $results->{query_href},
        MY_URL          => $cgi->script_name,

        HITS            => $results->navigation('hits'),
        FROM            => $results->navigation('from'),
        TO              => $results->navigation('to'),
        SHOWING         => $results->navigation('showing'),

        PAGES           => $results->navigation('pages'),
        NEXT            => $results->navigation('next'),
        NEXT_COUNT      => $results->navigation('next_count'),
        PREV_COUNT      => $results->navigation('prev_count'),
        PREV            => $results->navigation('prev'),

        RUN_TIME        => $results->header('run time') ||  'unknown',
        SEARCH_TIME     => $results->header('search time') ||  'unknown',
        MOD_PERL        => $ENV{MOD_PERL},
        MENU        => $menuInfo,

    $params->{FILES} =  $results->results if $results->results;


Thanks for you help and suggestions!

Aaron Bazar
Received on Wed Jun 25 16:11:12 2003