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

I have a CGI page where I use repetitive HTML data such as this table:
<table> <tr> <td>Info</td> </tr> <tr> <td>Second Info stuff</td> </tr> <tr> <td>More data</td> </tr> </table>
Anyway to put all this into a variable or an include file? I know Cold Fusion has an include file for storing information and using it on web pages. Is there anything I can do like that in perl so I dont have to type in the above HTML 8 times on the same CGI page? Basically just put the HTML piece in a variable or include file to cut back writing the same HTML over and over again.

Replies are listed 'Best First'.
Re: Repetitive HTML data
by naChoZ (Curate) on Sep 24, 2003 at 13:18 UTC

      Just to help out Anony a little bit more ...

      All you need to do is save that table as a file ... say some_table.html. Then, in your 'main' HTML file you use the <tmpl_include> tag to 'pull in' your table:
      <body> stuff: <tmpl_include some_table.html> mo stuff: <tmpl_include some_table.html> etc. etc. </body>
      Read the docs for more, and be sure to check out HTML::Template Tutorial and 3Re: HTML::Template - complex sites for more info.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: Repetitive HTML data
by reyjrar (Hermit) on Sep 24, 2003 at 13:00 UTC
    Sure thing:
    my $table = <<EOT; <table> <tr> <td>Info</td> </tr> <tr> <td>Second Info stuff</td> </tr> <tr> <td>More data</td> </tr> </table> EOT
    -brad..
      Maybe I'm reading this wrong, but seems like you have multiple rows of stuff you want to display. How 'bout this:
      my $table = "<table>"; foreach my $row_of_stuff (@rows_of_stuff) { $table .= <<EOT; <tr> <td>$row_of_stuff</td> </tr> EOT } $table .= "</table>";
      Does this help?
Re: Repetitive HTML data
by neilwatson (Priest) on Sep 24, 2003 at 15:43 UTC
Re: Repetitive HTML data
by theAcolyte (Pilgrim) on Sep 26, 2003 at 04:38 UTC
    Not to rain on anyone's parade, but this is something you don't need perl for at all -- not unless you need different data on each line. If your looking to repeat an element over and over and its completely static you have two other good choices:
    • Use Server Side Includes. This requires they be turned on by your webhost. Many don't allow it for security reasons.
    • Use the EMBED tag. The only real problem is only mozilla supports it properly for embedding HTML docs
    • Use an IFRAME. This is prolly easiest solution
    • Use the funky method described below

    You can also do it with javascript... but then if the surfer has JS turned off... ouch?? So here's the JS way.

    1) Make a .js doc thats all one line like this:
    document.write("your HTML HERE");

    2) in your main HTML doc where you want that file to be included you put the following line:
    <script LANGUAGE="javascript" TYPE="text/javascript" SRC="include.js"></script>

    For even MORE fun ... change the SRC to include.pl and write a perl file that dumps out 1 line of javascript which reads:
    document.write("blahblahblah");

    and you have a dynamic include. :-)

    -Erik