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: Advanced:
HELP: seperate HTML and perl

 



checkmate2001
stranger

Jul 31, 2001, 12:19 AM

Post #1 of 6 (1924 views)
HELP: seperate HTML and perl Can't Post

Just wondering if I can seperate HTML and perl
code, store the HTML part in a seperate file and let the
HTML authoring guy who doesn't know any perl to edit
the interface of the web page?

Here is a smiple example.
file1: page.html
<html>
<body>
Hello, $user
</body>
</html>

file2: hello.pl
#!/path/to/perl

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

$user = "guest";

open PAGE, "<page.html" or die "can't open";
while(<PAGE>) {
# print the content of the page #
# QUESTION lies here #
}
close PAGE or die "can't close";

The question is how I can substitute '$user'
embedded in the html file with the value of a real perl
variable.

Thanks for your answer in advance.





mhx
Enthusiast

Jul 31, 2001, 1:14 AM

Post #2 of 6 (1923 views)
Re: HELP: seperate HTML and perl [In reply to] Can't Post

Use a pattern for all the values you want to substitute that can be easily distinguished from being HTML. This is not for Perl, mainly for the one who has to edit the file. Let's assume you have the following 'extended' HTML file:

Code
<html> 
<head><title>Welcome, [=USER=]</title><head>
<body><p>
Hello [=USER=],<BR>
Your last logon was [=LOGON=]<BR>
</p></body>
</html>

The [=...=] pattern was arbitrarily chosen, if there's any reason you don't like it, take whatever you want. If this file was called test.html, you could use the following script for displaying the HTML with substitutions:

Code
#!/bin/perl -wT 
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use strict;

my $file = 'test.html';

open FILE, $file or die "cannot open $file: $!\n";
my $html = do { local $/=undef; <FILE> };
close FILE;

my %values = (
USER => 'Marcus',
LOGON => scalar localtime time,
);

$html =~ s/\[=(\w+)=\]/$values{$1}/g;

print header, $html;

As you can see, I've used a hash table to store the values for USER and LOGON. This way, the whole substution fits into a short line:

Code
$html =~ s/\[=(\w+)=\]/$values{$1}/g;

There may be better solutions than this, but I don't do a lot of CGI scripting. So if anyone could come up with a better idea, please do.
Hope this helps.

-- Marcus


Code
s$$ab21b8d15c3d97bd6317286d$;$"=547269736;split'i',join$,,map{chr(($*+= 
($">>=1)&1?-hex:hex)+0140)}/./g;$"=chr$";s;.;\u$&;for@_[0,2];print"@_,"



checkmate2001
stranger

Aug 1, 2001, 12:55 AM

Post #3 of 6 (1907 views)
Re: HELP: seperate HTML and perl [In reply to] Can't Post

Good perl!
Thank u very much
: )



slycer
stranger

Aug 31, 2001, 7:32 AM

Post #4 of 6 (1879 views)
Re: HELP: seperate HTML and perl [In reply to] Can't Post

Another option to what mhx suggests above is the HTML::Template module.

It claims to do what you are looking for (though it allows you to have some *VERY* simple perl in the HTML - ie while loops etc). I have not used it myself, but have heard many good things about it.



kencl
User

Aug 31, 2001, 11:19 PM

Post #5 of 6 (1875 views)
Re: HELP: seperate HTML and perl [In reply to] Can't Post

... or you could just use a subroutine. Here's what I do for string replacements in HTML

Code
<html><head><title>%%variabletitle%%</title></head> 
<body bgcolor='#%%variablecolor%%'>...

I use %% before and after the replacement, but that is arbitrary just like mhx's example. Your Perl script then contains:

Code
$CoolTitle = 'Perl Is Awesome!';   $CoolColor = 'FF0000';  # red 
%Replacements = ('variabletitle' => $CoolTitle, 'variablecolor' => $CoolColor);
$ReturnPagePointer = &MergeTemplate('HTMLFileName.html', \%Replacements);
unless ($ReturnPagePointer) {
# handle merge failure
}
print "Content-Type: text/html\n\n";
print $$ReturnPagePointer;

#############
# PURPOSE merges a template file replacing %%stringidentifier%% in source html with data provided
# EXPECTS Two strings 1) the name of the template file and
# 2) a pointer to a hash containing identifiers as keys and string to be swapped in as values
# RETURNS A pointer to the merged file or 0 if it failed
sub MergeTemplate {
my ($TemplateFile, $Changes) = @_; my $text; my $templatedir = "../templates/";
chomp($TemplateFile);
if(!open(TEMPLATE, "<$templatedir$TemplateFile")) {
&LogFailure("$0 was unable to open the template file $templatedir$TemplateFile in sub MergeTemplate.");
return 0;
}
local $/; # slurp mode
$text = <TEMPLATE>;
close(TEMPLATE);
# replace quoted words with value in %$Changes hash
$text =~ s{ %%( .*? )%% }
{ exists( $Changes->{$1} ) ? $Changes->{$1} : "" }gsex;
return \$text;
}

Notice that the subroutine also removes any %%stringidentifier%%s you don't include in the replacement hash when calling the script (more accurately, it replaces them with nothing :). This can be quite useful, for example, when you may or may not want to display a message.

>> If you can't control it, improve it, correlate it or disseminate it with PERL, it doesn't exist!


PerlKid
stranger

Sep 5, 2001, 10:39 AM

Post #6 of 6 (1860 views)
Re: HELP: seperate HTML and perl [In reply to] Can't Post

I posted an earlier version of the module I've been writing for this several months ago in the regexp forum. The most recent version can be found at ftp://ftp.camelsoup.com/pub/parser/CamelSoup/Parse.pm. Instructions on it's use are available upon request. In addition to simple varialbe replacements, you can perfom file includes (and nested includes), conditional output (and nested condiitionals), and create new variabls from inside the template itself.

--Drew
http://www.camelsoup.com

Code
s;[\d\$&(\^)];;g+s;\.; ;g+s;(.)(..);$2$1;g+print,if$_='&61k4I.)l6il.edn7(K2e^ny$';


 
 


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

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