Gossamer Forum
Home : Products : DBMan SQL : Discussion :

template call to other cgi

Quote Reply
template call to other cgi
Hi,
Is it possible to call another cgi from WITHIN a template?

I have a little random phrase generator (rand_text.cgi) which uses server side includes. It is placed in my cgi-bin directory. But in a customised template the line:
<!--#exec cgi="/cgi-bin/rand_text.cgi"-->
has no effect. (Frankly I didn't think it would work!)
So how can I get a template to trigger and display the result of such a seperate cgi?
Thanks for all help on this
Charly
Quote Reply
Re: [charly] template call to other cgi In reply to
Hi Charly,

There is no way to call a cgi from within the templates.

Anyway, you can create either a Global template or a pm file ( it should be in Dbsql folder ) instead and then add <%global_name%> or <%Dbsql::pm_file::subroutine%> tag into your templates.

TheStone.

B.

Last edited by:

TheStone: Sep 12, 2002, 10:31 AM
Quote Reply
Re: [TheStone] template call to other cgi In reply to
Ok, thanks. So this the result, trying to adapt my (working) cgi into a global.
My plain text file 'random.txt' is in my cgi-bin directory.
My global is called 'random_phrase'
and is entered as follows (comments left in for your comprehension)

sub {

# random_phrase
# This is test global version of rand_text.cgi
# Procedure suggested by the Stone 13/9/2002
# The if error routines ommitted

$random_file = "/home/mysite/cgi-bin/random.txt";
$delimiter = "\n\%\%\n";

# Open the file containing phrases and read it in.
open(FILE,"$random_file") || &error('open->random_file',$random_file);
@FILE = <FILE>;
close(FILE);

# Join these lines from the file into one large string.
$phrases = join('',@FILE);

# Now split the large string according to the $delimiter.
@phrases = split(/$delimiter/,$phrases);

# Invoke srand; with a seed of the time and pid. If we are on a machine
# which doesn't put the pid into $$ (ie. Macintosh, Win NT, etc...), change
# this line to: srand(time ^ 22/7);
srand(time ^ $$);

# Now pluck our random phrase out of the @phrases array!
# but this only returns a number.
$phrase = rand(@phrases);

# Change this number into the text we want to return!
return $phrases[$phrase];

}

My template includes a call to this global ie. <%random_phrase%>

I get an error message "Unable to compile global"

Any suggestions? Thanks in advance.
Quote Reply
Re: [charly] template call to other cgi In reply to
You missed 'my' commands to define the variable, so your code should be:

sub {

# random_phrase
# This is test global version of rand_text.cgi
# Procedure suggested by the Stone 13/9/2002
# The if error routines ommitted

my $random_file = "/home/mysite/cgi-bin/random.txt";
my $delimiter = "\n\%\%\n";

# Open the file containing phrases and read it in.
open(FILE,"$random_file") || &error('open->random_file',$random_file);
my @FILE = <FILE>;
close(FILE);

# Join these lines from the file into one large string.
my $phrases = join('',@FILE);

# Now split the large string according to the $delimiter.
my @phrases = split(/$delimiter/,$phrases);

# Invoke srand; with a seed of the time and pid. If we are on a machine
# which doesn't put the pid into $$ (ie. Macintosh, Win NT, etc...), change
# this line to: srand(time ^ 22/7);
srand(time ^ $$);

# Now pluck our random phrase out of the @phrases array!
# but this only returns a number.
my $phrase = rand(@phrases);

# Change this number into the text we want to return!
return $phrases[$phrase];
}

B.
Quote Reply
Re: [TheStone] template call to other cgi In reply to
Many thanks indeed. Works fine. Very pleased!
Charly
Quote Reply
Re: [charly] template call to other cgi In reply to
Just adding a small comment: if you're using the fortune files, you may want to consider putting all the data into an SQL database. That way, you won't have to load the entire fortune file every time someone hits that particular page... those files tend to be quite large and it would take up a large amount of resources should you get many requests at once.
Quote Reply
Re: [Aki] template call to other cgi In reply to
I don't know about "the fortune files" - no, I DO actually READ a lot (real books, not just perl code), and as a journalist also come across some interesting quotes, so I like to think its original material and what's more it's adapted to the topics covered in my site. But thanks for the advice!