CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Template driven webpage

 



panamajacksoy
Deleted

Apr 22, 2001, 7:56 PM

Post #1 of 5 (562 views)
Template driven webpage Can't Post

Hello to all.

I'm trying to make this script work I have tried it all with no luck.

Its suppose to make a webpage in a certian directory.

It works fine as long as I dont add anything (make a blank page) but once I add content forget it.

Ultimately I would like to be able to pull a template (http://www.panamas.net/cgi-script/guest/1test.txt), fill out a form (http://www.panamas.net/cgi-script/guest/makepage.html) and the script replace the follwoing fields in the txt file
headline
graphic
pagename

and save it as pagename.shtml

Here is the code I'm currently using. Please let me know if you have the soultion to it.
I have to make well over 5000 pages using this script or something similar using a template.

here is the script.

#!/usr/bin/perl

# These variables need to be set

# Set this to your base HTML directory. This is a PATH not a URL
$base_dir = "/home2/panamas/www/ecards/";

# This is your URL of where the new HTML pages will be kept
$baseurl = "http://www.panamas.net/ecards/";

# This is the path for user pages. You don't really need to change this
# Just make sure to create a directory: userpages and chmod it 777
$page_dir = "$base_dir/";

# This is a URL for the images sub directory in the userpages directory.
# Create the images directory in the userpages directory. This is where
# you will upload your background images
$image_dir = "/ecards/images/";

# That's it.


######################################################################################

# Get the input
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});

# split the input
@pairs = split(/&/, $input);

# split the name/value pairs
foreach $pair (@pairs) {

($name, $value) = split(/=/, $pair);

$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<([^>]|\n)*>//g;

$FORM{$name} = $value;
}

# Lets do some translating first
$headline = $FORM{'headline'};
$graphic = $FORM{'graphic'};
$pagename = $FORM{'pagename'};

# Find out what the user wants to do
if ($FORM{'action'} eq "Create Page") {
&create;
}

sub create {

# if the user tries to name their page
# something that is already taken
# this will HOPEFULLY stop them :)
# This block was written by Norm
if (-e "$page_dir$pagename\.shtml") {
print "Content-type: text/html\n\n";
print "<html><head><title>Error</title></head>\n";
print "<body><p>The page name: <b>$pagename</b>\n";
print "is already taken.\n";
print "Please go back and re-name your page</p>\n";
exit;
}

#now, lets create our new html page
open(HTML, ">$page_dir$pagename\.shtml") || die "I can't create that file\n";
print "<html><head><title>$pagename</title></head>\n";
print "<body><p>The page name: <b>$pagename</b></body></html>\n";

close(HTML);

# Give the user a response
print "Content-type: text/html\n\n";
print "<html><head><title>thanks</title></head>\n";
print "<body bgcolor=\"#ffffff\" TEXT=\"#000000\" link=\"#008000\" vlink=\"#800040\">\n";
print "Your page was created, and you will receive an e-mail confirming this!\n";
print "Your URL is: <a href=\"$baseurl$pagename\.shtml\">\n";
print "$baseurl$pagename\.shtml</a>\n";
print "Thanks for your parcipitation!\n";
print "</body></html>\n";
}



localfilmmaker
stranger

Apr 23, 2001, 3:03 PM

Post #2 of 5 (548 views)
Re: Template driven webpage [In reply to] Can't Post

Ok, it looks like the script breaks at these lines:

Code
#now, lets create our new html page 
open(HTML, ">$page_dir$pagename\.shtml") || die "I can't create that file\n";
print "<html><head><title>$pagename</title></head>\n";
print "<body><p>The page name: <b>$pagename</b></body></html>\n";
close(HTML);

The problem is that the two print statements are not printing to the file just created, but rather back to the browser. To fix this do this:


Code
#now, lets create our new html page 
open(HTML, ">$page_dir$pagename\.shtml") || die "I can't create that file\n";
print HTML "<html><head><title>$pagename</title></head>\n";
print HTML "<body><p>The page name: <b>$pagename</b></body></html>\n";
close(HTML);

And to make it so you can use a template file try this in place of the previous code:

Code
# Set the path to the template file 
my $template_file = "template.txt";

# Open and read in the template
open(IN, "< $template_file") or die "Cannot open template file: $!";
my @template =<IN>;
close(IN);

# Now lets put in the real stuff.
# In the template file we look for words to replace. These need to match exactly in the
# template to the ones listed below, in order to be replaced.
my @words = ('headline', 'graphic', 'pagename');
foreach my $word (@words) {
map { s/$word/$FORM{$word}/; } @template;
}

#now, lets create our new html page
open(HTML, ">$page_dir$pagename\.shtml") || die "I can't create that file\n";
map { print HTML $_; } @template;
close(HTML);

I think this will work. If there are mistakes, I'm sure some one will find them. Smile


-localfilmmaker
Spencer Christensen
spencer@mecworks.com


BigRich
Novice

Apr 25, 2001, 7:42 AM

Post #3 of 5 (530 views)
Re: Template driven webpage [In reply to] Can't Post

Throw that script away. It's not suited for a busy production environment. You're asking for trouble if you us it on a busy site.

I've attached a script that will let you use templates and it's made up of code that I've used on sites that have extremely heavy volume and I've had no problems or complaints.

It's well commented and basically pretty simple. Check out the docs for CGI.pm as well as perlsec and perlrun for more info.

Also, you may need to tweak the regular expressions for checking the values of the "headline" and "graphic" fields.


Good Luck,

BigRich



Cure
User

Apr 29, 2001, 9:05 AM

Post #4 of 5 (514 views)
Re: Template driven webpage [In reply to] Can't Post

use the CGI.pm to parse your data....

perldoc CGI


Cure



BigRich
Novice

Apr 30, 2001, 5:34 AM

Post #5 of 5 (501 views)
Re: Template driven webpage [In reply to] Can't Post

Errrr, I did. Near the beginning of the sample script I've attached you'll see a line with "use CGI;". That is how you use CGI.pm in a script, perhaps you should read perldoc CGI, or maybe even take a gander at the post you are answering. If you had, you'd have seen that I recommended that the op "Check out the docs for CGI.pm".


BigRich


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives