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: Beginner:
Template Question

 



etheracide
journeyman

Oct 14, 2005, 5:47 PM

Post #1 of 9 (1093 views)
Template Question Can't Post

I am using my own form of templating which is working like a charm however...it works only if all of the html is on a single line. My code is below... is there anything I can add/change to be able to allow html to flow over many lines as is normal with html files?


Code
print "Content-type: text/html\n\n"; 

open(TEMP, "./test/test.html") || &error;
$template = <TEMP>;
close(TEMP);

$template =~ s/%%title%%/This is my title!/g;
$template =~ s/%%text%%/This is the body text of my file. Woohoo!/g;

print "$template";
sub error {
print "That file could not be opened!"; exit;
}


This isn't essential necessarily but I'd like for downline users to not have to worry about making their HTML file be on one line. Any and all help is greatly appreciated!


KevinR
Veteran


Oct 18, 2005, 1:17 PM

Post #2 of 9 (1088 views)
Re: [etheracide] Template Question [In reply to] Can't Post

use slurp mode to open and read the html file into a scalar, it can be done a few different ways but here is one way:

open(TEMP, "./test/test.html") || &error;
$template = do { local $/; <TEMP> };
close(TEMP);
-------------------------------------------------


etheracide
journeyman

Oct 18, 2005, 5:09 PM

Post #3 of 9 (1086 views)
Re: [KevinR] Template Question [In reply to] Can't Post

Thanks a ton. Works like a charm!


etheracide
journeyman

Nov 8, 2005, 11:55 PM

Post #4 of 9 (1058 views)
Re: [etheracide] Template Question [In reply to] Can't Post

Every solution seems to lead to another question.

I am using the same line of code as in my first post in this thread and it works perfectly by replacing my created "tags" with the value of the scalar which I replacing the tag. However...is there a way that I could make the script run a subroutine if the given tag is found in the template instead of a scalar? The following doesn't work but it might give a bit of a clue as to what I am going for:


Code
$template =~ s/%%message_box%%/subroutine_name();/g;


A use for this could be for me to use the tag %%usermenu%% which would run a subroutine which prints out a bunch of html that creates a menu with links...which I can't seem to do with using a scalar.

Sorry for not putting this thread to bed yet, but as always..all help is grealty appreciated!


KevinR
Veteran


Nov 9, 2005, 12:19 AM

Post #5 of 9 (1057 views)
Re: [etheracide] Template Question [In reply to] Can't Post

yes you can. You need to use the 'e' option on the end of the regexp (like the g option you have) .

$template =~ s/%%message_box%%/subroutine_name()/eg;
-------------------------------------------------


etheracide
journeyman

Nov 11, 2005, 5:38 PM

Post #6 of 9 (1037 views)
Re: [KevinR] Template Question [In reply to] Can't Post

After adding that, is there any particular reason why the browser ends up printing the subroutine (well what the subroutine ends up telling the browser to print) BEFORE the rest of the html in which the subroutine should be printed?

The html source shows that what I am telling the subroutine to print to the browser is printed and then it starts printing the html from the template instead of inserting it into the html template where it should be like the scalar replacements are doing.


KevinR
Veteran


Nov 11, 2005, 10:32 PM

Post #7 of 9 (1034 views)
Re: [etheracide] Template Question [In reply to] Can't Post

all I can say is your perl script is doing what it's coded to do. If it's printing something before you think it should, then you have to look in the code and figure out why. If the code isn't too long post it here and someone will take a look.
-------------------------------------------------


etheracide
journeyman

Nov 12, 2005, 8:07 AM

Post #8 of 9 (1032 views)
Re: [KevinR] Template Question [In reply to] Can't Post

The CGI file's code:


Code
#!/usr/bin/perl 


use CGI;
$christophermichael = new CGI();
use CGI::Carp qw(fatalsToBrowser);

$entry = $christophermichael->param('entry');

print "Content-type: text/html\n\n";


open(TEMP, "./view_comments_template.html");
$template = do { local $/; <TEMP> };
close(TEMP);

$template =~ s/%%comments%%/&show_comments;/eg;
sub show_comments {

open (COMMENTS, "./comments/$entry.cms");
@comments = <COMMENTS>;
close (COMMENTS);

foreach $comment (@comments) {
($name, $email, $message) = split(/\|/,$comment);

print qq~
<table>
<tr>
<td><span class=body_bold>Name:</span></td>
<td><span class=body>$name</span></td>
</tr>
<tr>
<td><span class=body_bold>E-mail:</span></td>
<td><span class=body><a href="mailto:$email">$email</a></span></td>
</tr>
<tr>
<td><span class=body_bold>Comment:</span></td>
<td><span class=body>$message</span></td>
</tr>
</table>
<br />
~;

}

}
print "$template";


Now the HTML file with the template 'tag' I've created.


Code
<html> 
<head>
<title>View Feedback</title>
<style>
.title {font-family: Tahoma, Verdana; color: #ccccdd; font-size: 14px}
.body {font-family: Tahoma, Verdana; color: #ffffff; font-size: 11px}
.body_bold {font-family: Tahoma, Verdana; color: #ffffff; font-size: 12px}
.button {font-family: Tahoma, Verdana; color: #ffffff; font-size: 12px; border: 1px solid #ffffff; background-color: #000000}
</style>
</head>

<body bgcolor="#000000">

<span class=body>
<p>
Below are all of feedback posts available.
</p>
</span>
%%comments%%


</body>
</html>


I was thinking that in the other scripts where I use replace with scalars, the scalars are already defined just as $entry is already defined before I do the replace and therefore the value is available to be posted before I print $template. So I put the subroutine before the printing of $template in hopes that the print statement that results from the subroutine would appear INSIDE the template where it should be. But it still prints out the HTML from the subroutine and then prints the html template.


KevinR
Veteran


Nov 12, 2005, 1:24 PM

Post #9 of 9 (1029 views)
Re: [etheracide] Template Question [In reply to] Can't Post

your code is just printing the html that the sub routine creates when you call the sub routine in your regexp. Then you print $template. What you need to do is return the string back to where it was called from, in the regexp, instead of printing it, then print $template. Since you are looping through a file to create the html code, you want to use a variable to concatinate the loop iterations together into one continuous string. It could be done like so:


Code
$template =~ s/%%comments%%/show_comments()/eg;  
sub show_comments {
open (COMMENTS, "./comments/$entry.cms");
my @comments = <COMMENTS>;
close (COMMENTS);

my $temp = '';
foreach my $comment (@comments) {
my ($name, $email, $message) = split(/\|/,$comment);
$temp .= qq~
<table>
<tr>
<td><span class=body_bold>Name:</span></td>
<td><span class=body>$name</span></td>
</tr>
<tr>
<td><span class=body_bold>E-mail:</span></td>
<td><span class=body><a href="mailto:$email">$email</a></span></td>
</tr>
<tr>
<td><span class=body_bold>Comment:</span></td>
<td><span class=body>$message</span></td>
</tr>
</table>
<br />
~;
}
return($temp);
}
print "$template";


you should be using "strict" also with your perl scripts.
-------------------------------------------------

 
 


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

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