Skip to main content.
home | support | download

Back to List Archive

Re: cgi question/template questions

From: Bill Moseley <moseley(at)>
Date: Thu Jun 26 2003 - 16:10:30 GMT
On Thu, Jun 26, 2003 at 08:00:39AM -0700, Aaron Bazar wrote:

> 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.

Extract out the words from the query submitted, do some lookup on 
those words to map them to other keywords and then display those 
keywords.  Is that about right?

> 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).

perldoc perltoot is a good place to start.  OO in perl is reasonably 
straight forward and nothing too esoteric is used in these modules.

If you are just starting out and you are want to use a templating 
system, I'd recommend Template-Toolkit over HTML::Template.  That's just 
my preference (I use both, but mostly HTML::Template for simpler 

Basically, I find that with HTML::Template I do too much formatting and
adjusting of the data going into the template variables, where with
Template-Toolkit I just make the raw data available in the template and
then use the template to format it.  Maybe I'm just now using
HTML::Template well, but I find I always have a section of code that's
copying data from its source to a template variable.  In TT you can also
set a template variable to an object and make method calls (or
subroutine calls) back into code from the template.  I'm add a note
below about this below[1].

> 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

Ok, so you have a function that creates $menuInfo which is an array ref 
(or some such structure).  You assign it to $params{MENU} so the 
template can display it.

> I can't figure out how to get query_simple into the program...

You mean you can't see how to get the keywords into your function that 
creates $menuInfo?

> In the list of $params, I see "QUERY_SIMPLE    => CGI::escapeHTML(
> $results->{query_simple}"
> 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...

I suppose I'm not following now.

So you create a subroutine that you pass in the query string and it 
returns a list of keywords ($menuInfo from above):

sub create_keywords {
   my $query_string = shift;

   return \@list_of_keywords;

Then just add in a call to your subroutine:

        %params = ( %params,
            FILES           => $results->results,
            HITS            => $results->navigation('hits'),

            # Create my template variable of keywords:
            MENU            => create_keywords( $results->{query_simple} ),


[1] About Template-Toolkit -- maybe HTML::Template can do this also, but 
with TT you could do:

            MENU            => \&create_keywords,

and then in the template call that subroutine passing in the value of 

Now you may want to HTML escape your keywords if it's possible there 
could be HTML markup chars.

Now, there's a module called "ParseQuery" called in swish.cgi (search 
for parse_query) that splits out a swish-e query string into individual 
words (and phrases).  You might find that to be useful in your 
"create_keywords" function.  It might be already available as 
"$results->{parsed_query}", but I'm not sure.  Using the Data::Dumper 
module is a good tool to use for debugging.       

Bill Moseley
Received on Thu Jun 26 16:10:39 2003