Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML Snippet into Perl

Status
Not open for further replies.

DonP

IS-IT--Management
Jul 20, 2000
684
US
I have a little bit of HTML code (a footer) that I want to call into the appropriate parts of several scripts at the botton of other HTML code. I thought I could just PRINT it there but that just prints the path - as expected, now that I think about it. So I tried to REQUIRE is but that does nothing either. It is setup as a variable simply called footerfile. This should be easy! What am I missing here?

I tried renaming the actual file itself to footer.pl rather than footer.tem but it made no difference.

Don

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
If the file you are trying to include is simply an HTML chunk, you could just read it in and print it to the browser as you get it.....

$footer = '/some/path/to/the/footer.tem';
open(FTR,&quot;<$footer&quot;) or die &quot;Failed to open footer, $!\n&quot;;
while (<FTR>) { print; }
close IPF;


hope this helps...




keep the rudder amid ship and beware the odd typo
 
Thanks!

Yes, it is simply an HTML chunk without any Perl code, though I can add some if it would help. Maybe just some [tt]print &quot;some HTML to the browser&quot;;[/tt] would help.

But your code is exactly what I was looking for. After trying it and viewing the resulting source code in the browser, nothing printed from the last line just above the code and it does not die. Here is a simplified version:

[tt]sub myMember {
my($errmsg1) = shift;

print &quot;Content-type: text/html\n\n&quot;;
print &quot;<HTML><HEAD><TITLE>Title</TITLE>\n&quot;;
print &quot;</HEAD>\n<BODY>
print &quot;<CENTER><FONT SIZE=+2>Some text and other lines</FONT></FONT></CENTER>\n&quot;;

$footer = '$footerpath/footer.txt';
open(FTR,&quot;<$footer&quot;) or die &quot;Failed to open footer, $!\n&quot;;
while (<FTR>) { print; }
close IPF;

print &quot;</BODY></HTML>\n&quot;;
exit;
}[/tt]

So before I pressed this &quot;Submit Post&quot; button to say it didn't work, I played around with it a bit more. I tried several things but none worked. Then I moved the included file into the same directory as the script, which is not where I want really it, but with it there, it works perfectly! I know I had the path right, but for some reason the script didn't like it. So with this, it works and I will deal with the path issue later. Thanks a bundle!

[tt]sub myMember {
my($errmsg1) = shift;

print &quot;Content-type: text/html\n\n&quot;;
print &quot;<HTML><HEAD><TITLE>Title</TITLE>\n&quot;;
print &quot;</HEAD>\n<BODY>
print &quot;<CENTER><FONT SIZE=+2>Some text and other lines</FONT></FONT></CENTER>\n&quot;;

$footer = 'footer.txt';
open(FTR,&quot;<$footer&quot;) or die &quot;Failed to open footer, $!\n&quot;;
while (<FTR>) { print; }
close IPF;

print &quot;</BODY></HTML>\n&quot;;
exit;
}[/tt]

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
One more thing I noticed after getting it set up: this footer contains hyperlinks and on this site, there is a session variable ($session) that needs to be appended to the end of each hyperlink for it to work. If it is not, the logon screen will appear. The script itself has the variable and it is being used elsewhere on other HTML that it creates but it does not seem to be getting passed into this included file. Can you think of a way to do it?

Don

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Sure,

Change your HTML include file to contain some easily found chunk that you want to replace. Then, as you read lines from the include file to print them to the client, check each line and replace as needed.

If your include text contains
<A HREF='some_URL_here[red]<replace_This>[/red]'>Some text</A>.......
Code:
$footer = 'footer.txt';
open(FTR,&quot;<$footer&quot;) or die &quot;Failed to open footer, $!\n&quot;;
while ( $line = <FTR>) 
    {
    # if the line contains '<replalce_This>', replace with your var.
    $line =~ s/<replace_This>/$your_variable/;
    print $line;
    }
 close IPF;

'hope this helps




keep the rudder amid ship and beware the odd typo
 
Thanks! Do I want to be sure that the included file uses the <> around the variable? Without it as it is now, it was still loading with nothing. I think it should work once I figure out how to implement it!

Don

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Your code seems to be making a replacement but the session ID is not getting there. I am able to print it to the screen in upper part of the generated HTML above the header by using a simple print command, but it is not getting in the included footer file code. I tried putting a $session = $session above the $footer = line as I've found before that that sometimes works but it didn't make any difference this time. I also tried giving the variable a different name, $sessionid = $session but that didn't work either. Without going through the whole thing, can you think of any reason it is getting lost? Thanks again!

Don

Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Sorry, I guess the '<' and '>' were bad delimiters to use in my example. I only wanted to set off the string that you want to replace. If your HTML snippet (include chunk) contains....

<A HREF='[red]PlaceHolder[/red]'>Some text</A>

then, as you read line from the include file,
your replace statement would look for '[red]PlaceHolder[/red]' and replace it with your previously set $variable.

$line =~ s/[red]PlaceHolder[/red]/$variable/;

'hope this helps...





keep the rudder amid ship and beware the odd typo
 
It works like a charm! Thanks so much! :)

While we're at it, is it easy to add another variable without much more work?

For reference, this is what I ended up with:

[tt]
$footer = 'footer.txt';

open(FTR,&quot;<$footer&quot;) or die &quot;Failed to open footer, $!\n&quot;;
while ( $line = <FTR>)
{
# if the line contains &session, replace with your var.
$line =~ s/&session/&session=$session/;
print $line;
}
close IPF;
[/tt]


Don
don@ctagroup.org
Experienced in HTML, Perl, VBScript, PWS, IIS and Apache. Run OS/2 Warp 4, BeOS v5 and Windows NT (only when I have to!)
 
Trivial - add a second place holder in your include chunk and use a second regex replace statement after the existing one to do the second replace. Just be careful that you are always getting unique patterns for you replacements. You don't want your replace statements to swap the wrong part of your URL.

'Glad it's working;^)




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top