Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to insert some simple html files into another html file that I use as a template and I am not having any luck, I am read in the $input_temp and the substitution displays the path and not the content of the files, how could I make this thing work, any help or suggestions would be great!
I included samples of the html files so you can understand what I am trying to do, thanks for looking!

#!/perl/bin/perl use strict; use CGI qw(-oldstyle_urls :standard); use CGI::Carp qw( fatalsToBrowser set_message ); print "Content-type: text/html\n\n "; my $input_temp = "../html/main.html"; my $include1="../includes/header.html"; my $footer1="../includes/footer.html"; open(IN, $input_temp) || print "Can't open input file1: $input_temp\n" +; while(<IN>) { if ($_ =~ /\<INCLUDE1\>/) { $_ =~ s/\<INCLUDE1\>/$include1/; } if ($_ =~ /\<INCLUDE2\>/) { $_ =~ s/\<INCLUDE2\>/$footer1/; } } close IN; __MAIN.HTML__ <html> <head> <title>Simple Test</title> </head> <body> <INCLUDE1> <div class=menu> ...stuff... </div> <div class=main> <!--content--> </div> <FOOTER> </body> </html> __HEADER.HTML___ home - about - services - etc __FOOTER.HTML___ its all about service etc

Replies are listed 'Best First'.
Re: Reading in html files help!
by almut (Canon) on Apr 22, 2010 at 14:43 UTC
    the substitution displays the path and not the content of the files

    You have to read in the content of the files, e.g.

    my $fname_include1 = "../includes/header.html"; my $include1; { open my $fh, "<", $fname_include1 or die "Couldn't open include '$fnam +e_include1': $!"; local $/ = undef; $include1 = <$fh>; }
Re: Reading in html files help!
by wfsp (Abbot) on Apr 22, 2010 at 14:40 UTC
    You could let HTML::Template do the heavy lifting.

    This assumes the two files header.tmpl and footer.tmpl exist.

    #!/usr/bin/perl use warnings; use strict; use HTML::Template; my $t = HTML::Template->new(filehandle => *DATA); print $t->output; __DATA__ <html> <head> <title>Simple Test</title> </head> <body> <TMPL_INCLUDE NAME="header.tmpl"> <div class=menu> ...stuff... </div> <div class=main> <!--content--> </div> <TMPL_INCLUDE NAME="footer.tmpl"> </body> </html>
    <html> <head> <title>Simple Test</title> </head> <body> home - about - services - etc <div class=menu> ...stuff... </div> <div class=main> <!--content--> </div> its all about service etc </body> </html>
      I don't think I can use this because there is a lot of things happening inside of the while loop.
Re: Reading in html files help!
by choroba (Cardinal) on Apr 22, 2010 at 14:36 UTC
    You substitute the <INCLUDE?> tag for the path, so you see the path in the result. You already opened the input file, so just read its content and print it.
      ..how could I do that and not get the path instead?