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:
Replace Text in HTML Template

 



lumpster
Novice

Aug 7, 2002, 3:46 PM

Post #1 of 18 (2555 views)
Replace Text in HTML Template Can't Post

Howdy, Yes, i am a beginner to perl, and yes I searched the forums for about 30 minutes before posting. It's a real simple question that I'm sure some of you veterans are tired of hearing, but I'm a newbie and I can't seem to get a simple concept down. And hopefully this will help others...

Here's my question:

I want to create a script that takes an html template(in this case "template.shtml") and open it, and read it. Then find particular text, and replace it.

Here's a quick example:

template.shtml
----------------------------------------------------------

<html>
<head>
<title>Template</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">

<!--start here-->

</body>
</html>

------------------------------------------------------------

All the perl program will do(in this case "replace.pl") is open the HTML document, making it string, search the string for "<!--start here-->", then replace it with another string of HTML.

------------------------------------------------------------

replace.pl

------------------------------------------------------------

(Shebang yadda)......

$string = "fake info";

$string = "more fake info";

open(HTML, "template.shtml");
(@htmls) = <HTML>;
close(HTML);

foreach $html (@htmls) {
if ($html =~ /<!--start here-->/i) {

print $string;
print $string2;

}
}

I tried this, but it doesn't seem to work. After all the reading I've done, I seem to be stumped. If you could help it would be cool. Here's what I want the outcome to look like:

template.shtml on the "fly"
----------------------------------------------------------

<html>
<head>
<title>Template</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">

fake info
more fake info

</body>
</html>

Pirate This is your brain on perl? Any questions.


NuclearClam
Novice

Aug 8, 2002, 4:07 AM

Post #2 of 18 (2546 views)
Re: [lumpster] Replace Text in HTML Template [In reply to] Can't Post

i'm a perl n00b, so i hope my solution isn't wrong Laugh you can modify the string that it's looking for to what you need. mostly what i see wrong is that when you print, you're not printing to the file. you need to type print HTML "changes". but i attached what i would do anyway, mostly because i'm bored Smile. a better solution (and what i did in the second example) to what i assume your problem is, would be to ask for a filename with the default contents being that of template, then replace the requested text with the stuff from another file, and create a new file, rather than modify the actual template or replacement file themselves. just in case you want to make a change to either Smile.


Code
   

my $search = '<!-- start here -->';

my $replace = "fake info is fun";

open(HTML,">template.shtml") or die "$!"; #open it for writing.

$file .= $_ while(<HTML>); # puts it into a string like you wanted

$file =~ s/$search/$replace/;

print HTML $file;

close(HTML);



and now what i would recommend you do if you're making a set of pages off of one template...


Code
   

my $replacetext = '<!-- start here -->';



print "File that is to have the template applied?: ";

chomp ($newfile = <STDIN>);




open(TEMPLATE, "template.shtml") or die "$!";

$template .= $_ while(<TEMPLATE>);

close(TEMPLATE);



open(FILE, "$newfile") or die "$!";

$newfile .= $_ while(<FILE>); # yes i am rather fond of this construct :0)

close(FILE)



$template =~ s/$replacetext/$newfile/; # the actual replacement



$newfile =~ /(.*?)\.(\w){3}/; # gets file-name and mime-type

$newfile = $1 . "-withtemplate.$2"; # file-withtemplate.*

open(NEW,">$newfile"); # creates it

print NEW $template;

close(NEW);



if there are any mistakes, please forgive me. i am but a n00b, which also explains my terrible programming style Wink


(This post was edited by NuclearClam on Aug 8, 2002, 4:09 AM)


davorg
Thaumaturge / Moderator

Aug 9, 2002, 1:17 AM

Post #3 of 18 (2539 views)
Re: [lumpster] Replace Text in HTML Template [In reply to] Can't Post


Code
my $string = "fake info"; 
my $string2 = "more fake info";

# always check the return code from open
open(HTML, "template.shtml") or die $!;

while (<HTML>) {
if (/<!--start here-->/i) {
print $string;
print $string2;
} else {
print;
}
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


lumpster
Novice

Aug 9, 2002, 1:50 PM

Post #4 of 18 (2534 views)
Re: [davorg] Replace Text in HTML Template [In reply to] Can't Post

Davorg I used your perl coding just because it looked shorter, and now I can get it working. But for some reason...and maybe you can answer this question, how come when I load my template.shtml (which has some Server Side Includes) it doesn't display my SSI? It displays all the images in the template. Hmm, do I need to save it to my server and then open it? Nah I can't do that because that would override the template. Hmm, let me know you thoughts.


davorg
Thaumaturge / Moderator

Aug 10, 2002, 1:39 AM

Post #5 of 18 (2524 views)
Re: [lumpster] Replace Text in HTML Template [In reply to] Can't Post

Can you explain exactly what you're trying to do?

You can't (easily) use SSI in a page that is generated from a CGI program.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


lumpster
Novice

Aug 10, 2002, 1:43 PM

Post #6 of 18 (2523 views)
Re: [davorg] Replace Text in HTML Template [In reply to] Can't Post

Here is the SSI I am using:

<!--#include file="topbar.txt" --> (spits out top article)
<!--#include file="sidebar.txt" --> (spits out side article)
<!--#include file="bottomnav.txt" --> (spits out bottom navigation)

<!--#config timefmt="%Y" --><!--#echo var="DATE_LOCAL"--> Organization Name. All rights reserved. (spits out 2002 Organization Name. All rights reserved)

Now I am using the top 3 includes because they are text with HTML that is going to be changed frequently. We add sections to our site very often so our navigation on the bottom has to be flexible also and since it changes so often we don't have time to update on every single page, hence the includes. The date function is something I could change easily, but it makes it easier not to have to worry about the year being current. Anyway, let me know what ya think or if I need to call it in perl instead of through the template.

Thanks for all your help! Wink


davorg
Thaumaturge / Moderator

Aug 11, 2002, 1:05 AM

Post #7 of 18 (2516 views)
Re: [lumpster] Replace Text in HTML Template [In reply to] Can't Post

Well, like I said, it's hard to get SSI working on pages that are generated using CGI. You'd be better off just reproducing that functionality in the CGI program.

In the first three cases you'd just open the file and print it out. In the last case you can use "localtime" to get the year.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Aug 11, 2002, 2:29 AM

Post #8 of 18 (2515 views)
Re: [davorg] Replace Text in HTML Template [In reply to] Can't Post

>>
it's hard to get SSI working on pages that are generated using CGI
<<

It's not possible ;)


davorg
Thaumaturge / Moderator

Aug 11, 2002, 2:53 AM

Post #9 of 18 (2514 views)
Re: [RedRum] Replace Text in HTML Template [In reply to] Can't Post


In Reply To
>>
it's hard to get SSI working on pages that are generated using CGI
<<

It's not possible ;)


Very few things are impossible in Perl.

One possible solution would be to have the CGI program write its output to a .shtml and then redirect to that file. That way the SSI directives would be interpreted.

I'm not saying I'd recommend that as a solution - simply that it's possible :)

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Paul
Enthusiast

Aug 11, 2002, 3:50 AM

Post #10 of 18 (2515 views)
Re: [davorg] Replace Text in HTML Template [In reply to] Can't Post

Thats a work around not a solution. Writing to a .shtml file and redirecting doesn't constitute as proof that SSI works with CGI generated html because it doesn't, you have simply just used SSI the normal way with a .shtml file.

>>
Very few things are impossible in Perl.
<<

Yep that's right, but SSI is server parsing so the flexibility of perl is irrelevant in this case.


(This post was edited by RedRum on Aug 11, 2002, 4:03 AM)


lumpster
Novice

Aug 11, 2002, 4:22 PM

Post #11 of 18 (2506 views)
Re: [RedRum] Replace Text in HTML Template [In reply to] Can't Post

Ah, so perl does have it's limits, hehe. Thanks for the quick replies, and I'll let you know if I find a solution. (That I can actually program on my own.. should be pretty easy). Hopefully this thread helps other newbies to perl realize some stuff about SSI, thanks once again.


Jasmine
Administrator / Moderator

Aug 14, 2002, 5:46 AM

Post #12 of 18 (2494 views)
Re: [lumpster] Replace Text in HTML Template [In reply to] Can't Post


Quote
Ah, so perl does have it's limits, hehe

Perhaps... perhaps not :)

A quick search on [url=http://search.cpan.org/search?mode=module&query=SSI]CPAN offered the following: Apache::SSI (download), Apache::FakeSSI (download), CGI::SSI (download) , and CGI::SSI_Parser (download).

Some samples from CGI::SSI's docs:


Code
    use CGI::SSI;  

$ssi = CGI::SSI->new();

print $ssi->flastmod(file => $filename);
print $ssi->include(virtual => '/foo/bar.footer');
print $ssi->exec(cgi => '/cgi-bin/foo.cgi');



Paul
Enthusiast

Aug 14, 2002, 12:09 PM

Post #13 of 18 (2488 views)
Re: [Jasmine] Replace Text in HTML Template [In reply to] Can't Post

>>
Perhaps... perhaps not :)
<<

Not meaning to be rude but you are missing the point.

These are _workarounds_ and not solutions. SSI will simply not work with CGI scripts. These modules just mimmick SSI and don't actually implement it.


(This post was edited by RedRum on Aug 14, 2002, 12:10 PM)


Jasmine
Administrator / Moderator

Aug 14, 2002, 2:58 PM

Post #14 of 18 (2479 views)
Re: [RedRum] Replace Text in HTML Template [In reply to] Can't Post

If the point was whether <!--#include file="/blah.txt"--> can be used in a cgi script, then yes, I've completely missed the point.

If the point was having files included into a cgi file, using date formats and echoing some variables, then I don't believe I missed the point. It's the big picture I'm concerned with, not necessarily the technical details.

True, SSI won't work in scripts, but the problem at hand was running SSI commands. It's pedantic to not consider readily available alternatives which will yield the same result. Whether or not it fakes SSI or really is SSI isn't the issue. The issue is what needs to be done, not how it is done.

The short, easy and technically correct answer is no, you can't use <!--#include file="/blah.txt"--> -- SSI doesn't work in cgi scripts, period -- (no alternatives offered). This doesn't help to solve the problem.

The longer and more helpful answer is no, you can't use <!--#include file="/blah.txt"-->, but you can use [ davorg ]'s suggestion earlier in this thread or a module that'll fake it and still do what needs to be done without having to duplicate the contents of your included files or write other code to emulate your SSI calls.

He needed this to be done:


Code
<!--#include file="topbar.txt" -->  
<!--#include file="sidebar.txt" -->
<!--#include file="bottomnav.txt" -->
<!--#config timefmt="%Y" --><!--#echo var="DATE_LOCAL"-->


This can be done from within his program with:


Code
use CGI::SSI;  

print $ssi->include(file=> 'topbar.txt') .
$ssi->include(file=> 'sidebar.txt') .
$ssi->include(file=> 'bottomnav.txt') .
'@' .
$ssi->config('timefmt', '%Y') .
$ssi->echo('DATE_LOCAL');


It may be a workaround that mimics SSI, but it sure looks like a solution.

Update: Fixed typo.


(This post was edited by Jasmine on Aug 14, 2002, 5:08 PM)


Paul
Enthusiast

Aug 14, 2002, 3:25 PM

Post #15 of 18 (2475 views)
Re: [Jasmine] Replace Text in HTML Template [In reply to] Can't Post


Quote
It's pedandic to not consider readily available alternatives which will yield the same result. Whether or not it fakes SSI or really is SSI isn't the issue. The issue is what needs to be done, not how it is done.


Indeed, I'm not criticizing the methods you provided to work around the issue, I just didn't want people to go away from this thread thinking that SSI worked with CGI scripts.


SaiyanM
User

Aug 14, 2002, 4:10 PM

Post #16 of 18 (2472 views)
Re: [RedRum] Replace Text in HTML Template [In reply to] Can't Post

an ssi code worked in my cgi program

Sly

-------------
Whats 2 + 2 x 2?
Its not 8...
(Please Excuse My Dear Aunt Sally)...


Jasmine
Administrator / Moderator

Aug 14, 2002, 5:10 PM

Post #17 of 18 (2469 views)
Re: [SaiyanM] Replace Text in HTML Template [In reply to] Can't Post

Oh? What code did you use? Or did the cgi create an shtml file that can parse the ssi call?


SaiyanM
User

Aug 14, 2002, 6:43 PM

Post #18 of 18 (2467 views)
Re: [Jasmine] Replace Text in HTML Template [In reply to] Can't Post

 Heres the script. It takes the header and footer and takes the query string and uses them

http://www.asdasd.com/?the_include_file_would_be_here

like that

-------------
Whats 2 + 2 x 2?
Its not 8...
(Please Excuse My Dear Aunt Sally)...
Attachments: layouts.pl (0.86 KB)

 
 


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

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