CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced:
HELP: seperate HTML and perl



Jul 31, 2001, 12:19 AM

Post #1 of 6 (2068 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
Hello, $user


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

Thanks for your answer in advance.


Jul 31, 2001, 1:14 AM

Post #2 of 6 (2067 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:

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

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:

#!/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:

$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



Aug 1, 2001, 12:55 AM

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

Good perl!
Thank u very much
: )


Aug 31, 2001, 7:32 AM

Post #4 of 6 (2023 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.


Aug 31, 2001, 11:19 PM

Post #5 of 6 (2019 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

<body bgcolor='#%%variablecolor%%'>...

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

$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/";
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>;
# 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!


Sep 5, 2001, 10:39 AM

Post #6 of 6 (2004 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 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.


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