How to Join Member's Area Private Library Search Today's Topics p Login
Main Forums Discussion Tech Talk Mature Content Archives
   Nav Win
 Tech Talk
 Beyond the Basics
 Using Templates
 1 2 3 4 5 6
Follow us on Facebook

 Moderated by: Ron   (Admins )

 
User Options
Format for Better Printing EMail to a Friend Not Available
Admin Print Send ECard
Passions in Poetry

Using Templates

 Post A Reply Post New Topic   Go to the Next Oldest/Previous Topic Return to Topic Page Go to the Next Newest Topic 
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


0 posted 05-31-2001 04:39 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher


Hello class!

My question today has to do with the use of templates. Specifically speaking this moment, in using a template to read into the PERL program, then to write out to HTML... So far, I've been able to accomplish part of this, using:

**********

     open(TEMPLATE, 'template.txt');
     while(<TEMPLATE> { print $_ }
     close(TEMPLATE);

**********

This works great here: http://countlesshorizons.com/test/form5-2.htm ... but as you can see from the output, I don't know how to include the variables from a template file... or rather, how to make them be read as variables.

I figure that worst case, I could do the template in two parts like such:

**********

     open(TEMPLATE1, 'header.txt');
     while(<TEMPLATE1> { print $_ }
     close(TEMPLATE1);

print $variable1;
print $variable2;
print $variable3;
print $variable4;
print $variable5;
print $etc;

     open(TEMPLATE2, 'footer.txt');
     while(<TEMPLATE2> { print $_ }
     close(TEMPLATE2);

**********

Where the top part prints out the beginning html, down to the beginning of a table data cell where the variable info would be printed, then finished up with the last one.

Is there another way though... maybe one where I can read in a template file and have the variables read from it?

Thanks

C

Code for above link:

HTML
**********

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Submit your Poems, Stories & Artwork</title>
<br><br>
<form method="post" action="../cgi-bin/test/test5-2.pl">
<div align="center">
<table width="500" bgcolor="#6699cc" cellpadding="0" cellspacing="0" border="0">
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><strong>Your Name</strong>
</td>  
   </tr>
   <tr>
    <td height="30" valign="top">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><input type="text" name="name" size="30" maxlength="50">
</td>  
   </tr>
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><strong>Your Email Address</strong>
</td>  
   </tr>
   <tr>
    <td height="30" valign="top">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><input type="text" name="email" size="30" maxlength="50">
</td>  
   </tr>  
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><strong>Category</strong>
</td>  
   </tr>
   <tr>
    <td height="30" valign="top">
<img src="imgs/1.gif" width="1" height="1" hspace="35">
<select name="category" size="1">
<option value="poetry">Poetry
<option value="prose">Prose
<option value="prosen">Prose Non-Fiction
<option value="essay">Essay / Article
<option value="other">Other
<option value="comments">Comments / Suggestions
</select>
<br>
</td>  
   </tr>
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><strong>Optional</strong>
</td>  
   </tr>
   <tr>
    <td height="30" valign="top">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><textarea name="bio" cols="42" rows="5">Brief description about yourself,
including prior publications
and/or homepage links.
If you have an existing bio, this will replace it.</textarea>
</td>  
   </tr>
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><strong>Title of Submission</strong>
</td>  
   </tr>
   <tr>
    <td height="30" valign="top">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><input type="text" name="title" size="30" maxlength="50">
</td>  
   </tr>          
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><strong>Submission</strong>
</td>  
   </tr>
   <tr>
    <td height="30" valign="bottom">
<img src="imgs/1.gif" width="1" height="1" hspace="35"><textarea name="sub" cols="42" rows="20">Please include any formatting, including HTML.</textarea>
</td>  
   </tr>
   <tr>
    <td height="10" valign="top">
<br>
    <div align="center"><input type="submit" value=" Submit "><input type="reset" value=" Reset "></div>
<br>
</td>  
   </tr>
</table>
</div>
</form>
<br>
</body>
</html>

**********
PERL
**********

#!/usr/local/bin/perl

#input in
&inputIn;
#sub to read and parse input and place into array
sub inputIn {
    if ($ENV{'REQUEST_METHOD'} eq 'GET')
    {$formInfo = $ENV{'QUERY_STRING'};}
    else
    {read(STDIN, $formInfo, $ENV{'CONTENT_LENGTH'});}
    @inputPairs = split (/[&;]/, $formInfo);
    %input = ();
    foreach $pair (@inputPairs) {
        $pair =~ s/\+/ /g;
        ($name, $value) = split (/=/, $pair);
        $name =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
        $value =~ s/%([A-Fa-f0-9]{2})/pack("c",hex($1))/ge;
        $input{$name} = $value;
        }
}

#get date
&getDate;
#sub gets date and time
sub getDate {
($sec,$min,$hr,$day,$month,$year,$day_of_week,$day_of_year,$some) = localtime(time);
@months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
#convert to 'real' year
$year = $year + 1900;
}

#enter how many hours to subtract from server
$zone = 3
#get time zone
&timeZone;
#sub for time zone
sub timeZone {
$hr = $hr - $zone;
}

#define variable names from form
$name  = $input{'name'};
$email = $input{'email'};
$bio  = $input{'bio'};
$category = $input{'category'};
$title  = $input{'title'};
$Sub = $input{'sub'};
$refURL = $ENV{HTTP_REFERER};

#fixes carraige returns for htm printout
$SubHTM = $input{'sub'};
$SubHTM =~ s/\n/<br>/g;
$SubHTM =~ s/\r//g;

#month/day/year/hour/minutes
$shortDate = "$months[$month] $day, $year at $hr:$min";

&catFind;
#sub to determine which editors to send to
sub catFind {
    if ($category eq 'poetry') {$email2 = 'poetry@countlesshorizons.com';}
    elsif ($category eq 'prose') {$email2 = 'prose@countlesshorizons.com';}
    elsif ($category eq 'prosen') {$email2 = 'prosen@countlesshorizons.com';}
    elsif ($category eq 'essay') {$email2 = 'essay@countlesshorizons.com';}
    elsif ($category eq 'other') {$email2 = 'other@countlesshorizons.com';}                
    elsif ($category eq 'comments') {$email2 = 'comments@countlesshorizons.com';}
    else {$email2 = 'UNKNOWN';}
}

#subject 1 = to submitter
#subject 2 = to editors
$subject1 = "Thank you for Submitting \"$title\" to Countless Horizons";
$subject2 = "$name has submitted the $category piece \"$title\" to CH";

#path to sendmail
$sendmail = "/usr/lib/sendmail -t -oi";

#send email to submitter
&sendMail1;

#sub for email
sub sendMail1 {
    open (MAIL, "| $sendmail") or
    die "Couldn't open sendmail: ";
    print MAIL "From: $email2\n";
    print MAIL "To: $email\n";
    print MAIL "Subject: $subject1\n\n";
    print MAIL "$Sub\n";
    close MAIL;
}    

#send email to editors
&sendMail2;

#sub for email2
sub sendMail2 {
    open (MAIL, "| $sendmail") or
    die "Couldn't open sendmail: ";
    print MAIL "From: $email\n";
    print MAIL "To: $email2\n";
    print MAIL "Subject: $subject2\n\n";
    print MAIL "$Sub\n";
    close MAIL;
}

#start page
&header;
open(TEMPLATE, 'template.txt');
while(<TEMPLATE> { print $_ }
    close(TEMPLATE);
#end page
&footer;

#subs for html header and footer
sub header {
    print "Content-type: text/html\n\n";
}    
sub footer {
    print qq~</body></html>~;
}

**********
TEMPLATE
**********

<html><head><title>Printing Variables</title></head><body>
         <p>Name of Submitter: <strong>$name</strong></p>
         <p>Email of Submitter: <a href="$email">$email</a></p>
         <p>Bio of submitter:<br><br><hr width="250" align="left"><strong>$bio</strong><br><hr width="250" align="left"><br></p>
         <p>Category Chosen: <strong>$category</strong></p>
         <p>Category Chosen 2: <strong>$email2</strong></p>
         <p>Title: <strong>$title</strong></p>
         <p>Sub w/o adj.: <i>$Sub</i></p>
         <p>Sub w/adj.: <br><br><hr width="250" align="left"><strong>$SubHTM</strong><br><hr width="250" align="left"><br></p>
         <p>Referring URL: <a href="$refURL">$refURL</a></p>
         <p>Time is: <strong>$shortDate</strong></p>

**********

Ron, if you get this far down, would you mind considering giving us this: [hr width=350] That would be appreciated!...though I could probably use a quote, huh...next time

Peace
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


1 posted 05-31-2001 07:57 PM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

I do this all the time, Chris. Once in a great while it gets tricky (usually when adding entire rows to a table), but usually it's pretty straight forward.

A fragment of the HTML template might look like this:

<input type="text" name="name" size="30" maxlength="50" value="**name**">
<input type="text" name="email" size="30" maxlength="50" value="**email**">

The matching Perl code then looks something like this:

open(TEMPLATE, 'template.txt');
@Page = <TEMPLATE>;
close(TEMPLATE);

foreach $line(@Page) {
  $line =~ s/\*\*name\*\*/$SenderName/;
  $line =~ s/\*\*email\*\*/$emailaddy/;
  print $line;
}
Kit McCallum
Administrator
Member Laureate
since 04-30-2000
Posts 16920
Ontario, Canada


2 posted 05-31-2001 11:08 PM       View Profile for Kit McCallum   Email Kit McCallum   Edit/Delete Message      Find Poems  View IP for Kit McCallum

Nothing important to add, just       I really do love this stuff, LOL.
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


3 posted 06-03-2001 02:18 AM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

OK - I haven't tried this yet, though I will in a moment. However, whether I can get it to work means little in my way of actually understanding it.

I think one of the downsides to not learning in a structured environment is that you may pass over some things which can be needed later.

Ok, I see that you've demarked the names of the form fields with the asterisks preceding and following... I am assuming that those can be any set of charaters, only needing to be matched by the following portion of code...?

Next, you open template.txt, assigning it the name of TEMPLATE, read it into the array "@page", then close the file since yu have it now.

Then you're telling it that for each line in the new array that matches the $page variable, to print out... you're saying that the $page = (then this part I don't wholly understand... what does the "s/" represent?) \* (backslash allowing the character instead of seeing it as 'multiply') name /$variableName/; (is this last part telling it to substitute the appropriate variable in place of the particular **email** name? I think so, but not wholly sure, since I don't know what the /s means

Anyway, thank you, I will go play with it now. Please let me know what that means and if I'm off track on anything!

Kit - glad someone besides Ron and I are enjoying playing around in here... it might get lonely otherwise!  

C
Ron
Administrator
Member Rara Avis
since 05-19-99
Posts 9708
Michigan, US


4 posted 06-03-2001 10:45 AM       View Profile for Ron   Email Ron   Edit/Delete Message      Find Poems   Click to visit Ron's Home Page   View IP for Ron

quote:
is this last part telling it to substitute the appropriate variable in place of the particular **email** name?


Yep. And now you should also know what the "s" represents…  

All of your other assumptions are correct, too.
Christopher
Moderator
Member Rara Avis
since 08-02-99
Posts 9130
Purgatorial Incarceration


5 posted 06-03-2001 07:24 PM       View Profile for Christopher   Email Christopher   Edit/Delete Message      Find Poems  View IP for Christopher

rockin'!!! One step closer to the holy grail.  

Cool - now I have to decide what to tackle next... hmmm... how about form validation and cookies... cool.

Thanks Ron!

C
Kit McCallum
Administrator
Member Laureate
since 04-30-2000
Posts 16920
Ontario, Canada


6 posted 06-03-2001 09:33 PM       View Profile for Kit McCallum   Email Kit McCallum   Edit/Delete Message      Find Poems  View IP for Kit McCallum

Great Chris!  I'll watch for the next class ... thanks for the lesson gentlemen!  
 
 Post A Reply Post New Topic   Go to the Next Oldest/Previous Topic Return to Topic Page Go to the Next Newest Topic 
All times are ET (US) Top
  User Options
>> Tech Talk >> Beyond the Basics >> Using Templates Format for Better Printing EMail to a Friend Not Available
Print Send ECard

 

pipTalk Home Page | Main Poetry Forums

How to Join | Member's Area / Help | Private Library | Search | Contact Us | Today's Topics | Login
Discussion | Tech Talk | Archives | Sanctuary



© Passions in Poetry and netpoets.com 1998-2013
All Poetry and Prose is copyrighted by the individual authors