Skip to main content.
home | support | download

Back to List Archive

Re: Adding new drop downs to TemplateHTMLTemplate.pl

From: Bill Moseley <moseley(at)not-real.hank.org>
Date: Thu Dec 11 2003 - 20:18:07 GMT
On Thu, Dec 11, 2003 at 11:22:31AM -0800, David Fishburn wrote:
> I wanted to add another drop down, called Language.
> 
> I am not having any success adding this new drop down, so I was hoping
> someone could give me some tips.

Well, it looks like you are on the right track.  All I can really add is
to make use of Data::Dumper and print things to STDERR to make sure you
have the data where you expect.  Basically:

   print STDERR Dumper $params;  # debug: what does HTML::Template get?
   
   $template->param( $params );
   my $page = $template->output;

   # debug before HTML::FillInForm processes:
   print STDERR $page

My only other comment (and this is for anyone using swish.cgi) is if you
are creating a very specialized search script you might consider writing
a new script.  swish.cgi contains a lot of extra overhead to make it
general purpose.  The concept that the config file can control how the
template is generated gives me a shudder.  The HTML widget choice is a
presentation issue, so that choice should just be in the template.


<rambling>
I'm also more of a Template-Toolkit fan.  Something bugs me about
creating the view hash (the has passed to HTML::Template's param
method).  Template-Toolkit is nice because you can just pass it whatever
(i.e. a SWISH::API results object) and have it generate the output.  I
do like the use of HTML::FillInForm to make sticky forms (although with
Template-Toolkit you can use CGI.pm directly in the templates for sticky
forms.  HTML::FillInForm allows more pure and simple HTML in the
template.
</rambling>

Here's a very rough example I posted a few weeks ago that doesn't do
highlighting or much else that swish.cgi does.  But it's a bare-bones
example to start with.

#!/usr/local/bin/perl -w
use strict;
use CGI;
use SWISH::API;
use Template;


use vars qw/$template $swish/;  # for caching under speedycgi/mod_perl  

process_request();

sub process_request {
    my $cgi = CGI->new;
    my $query = $cgi->param('query');
    my %params = (
        title => 'Company Name',
        index => 'index.swish-e',
        cgi   => $cgi,
    );

    if ( $query ) {
        my $start = $cgi->param('page') || 1;
        my $pagesize = 15;
        $params{search} = run_query( $query, $start, $pagesize, $params{index} );
    }


    $template ||= Template->new( INCLUDE_PATH => "$ENV{HOME}/apache" );
    die $template->error unless $template;
    print $cgi->header;
    $template->process( \*DATA, \%params ) || die $template->error;
}

sub run_query {
    my ($query, $page, $pagesize, $index) = @_;

    $page||= 1;
    $page = 1 unless $page =~ /^\d+$/;

    $pagesize ||= 15;
    $pagesize = 15 unless $pagesize =~ /^\d+$/ && $pagesize > 0 && $pagesize < 50; 

    $swish ||= SWISH::API->new( $index );
    die "Failed to create SWISH::API object" unless $swish;
    $swish->AbortLastError if $swish->Error;

    my $results = $swish->Query( $query );

    if ( $swish->Error ) {
        $swish->AbortLastError if $swish->CriticalError;
        return {
            query   => $query,
            message => join( ' ', $swish->ErrorString, $swish->LastErrorMsg ),
        };
    }


    $results->SeekResult( ($page-1) * $pagesize );

    my @records;
    my $result;
    my $cnt = $pagesize;
    while ( $cnt-- && ($result = $results->NextResult) ) {
        push @records, $result;
    }

    my %query = (
        query   => $query,
        results => \@records,
        hits    => $results->Hits,
        shown   => scalar @records,
        page    => $page,
        start   => ($page-1) * $pagesize,
    );

    $query{prev} = $page-1 if $page > 1;
    $query{next} = $page+1 if $query{start} + $pagesize < $query{hits}-1;

    return \%query;
}



<html>
<head><title>Search Documents</title></head>
<body>
    [% PROCESS form %]
    <p>
    [% IF search %]
        [% IF search.message; '<h2 align="center">'; search.message; "</h2>"; END %]

        [% IF search.shown %]
            [% PROCESS results_header %]
            [% PROCESS display_results %]
        [% END %]
    [% END %]
</body>



[% BLOCK form %]
    [% cgi.start_form( '-action' => cgi.script_name, '-method' => 'GET' ) %]
        [% cgi.textfield( {
            name    => 'query',
            size    => 40,
            maxlength => 200,
            } ) %]

        [% cgi.submit('submit','Search!') %]
    [% cgi.end_form.join('') %]

[% END %]

[% BLOCK results_header %]
<p>Showing page [% search.page %] 
([% search.shown %] of [% search.hits %] hits) 
for <b>[% search.query | html %]</b><br>

[% USE myurl = url( cgi.script_name, query=search.query ) %]

[% IF search.prev %]
   <a href="[% myurl( page=search.prev ) %]">Previous Page</a> 
[% END %]
[% IF search.next %]
   <a href="[% myurl( page=search.next ) %]">Next Page</a> 
[% END %]
<p>
[% END %]

[% BLOCK display_results %]
    [% USE date %]
    [% FOREACH item = search.results %]
        <dl>
            <dt>
                <a href="[% item.Property('swishdocpath') | uri | html %]">
                [% ( item.Property('swishtitle') || item.Property('swishdocpath') )  %]
                </a>

                <small>-- rank: <b>[% item.Property('swishrank') %]</b></small>
            </dt>

            <dd>
                [% item.Property('swishdescription') || "No description" | html %]
                <br>
                <small>
                    Path: [% item.Property('swishdocpath') | html %]
                    Size: [% item.Property('swishdocsize') %] bytes
                    Date: [% date.format( item.Property('swishlastmodified')) %]
                </small>
            </dd>
        </dl>
    [% END %]
[% END %]





-- 
Bill Moseley
moseley@hank.org
Received on Thu Dec 11 20:18:16 2003