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

Greetings,
I have a string of code <!-- INSERT TEXT HERE --> in an HTML document I will call index.shtml.
I want to substitute this string of code with the text of another text file I will call events2.txt.
What is the best way to do this?
Thanks,
Cal

Replies are listed 'Best First'.
Re: substituting strings in HTML with text files
by grep (Monsignor) on Jul 24, 2002 at 05:03 UTC
    To offer a more perlish (and flexable) solution. Template Toolkit offers a very simple way to accomplish your goal.

    Just create a template like this

    template.tmpl
    <html> <head> <title>HTML File</title> </head> <body> [% INCLUDE 'events.txt' %] </body> </html>

    The perl code looks like this

    use Template; use CGI; my $q = CGI->new(); my $t = Template->new({INCLUDE_PATH => 'template/'}); $t->process('template.tmpl') or die "Template process failed: ", $t->e +rror(), "\n";

    You can even put logic inside the template

    ... [% IF var = 'what I want' %] [% INCLUDE 'this_file.tmpl' %] [% ELSE %] [% FOREACH x = myarray %] <tr><td>[% INCLUDE x %]</td></tr> [% END %] [% END %] ...


    grep
    Just me, the boy and these two monks, no questions asked.
Re: substituting strings in HTML with text files
by Nightblade (Beadle) on Jul 24, 2002 at 02:52 UTC
    if it is .shtml file and server suports SSI, you can write
    <!--#include virtual="events2.txt" -->
      The user gets to the STHML page from a browser redirect I dont think you can execute from an already exected page.
        There is no "executed page" thing happening here. And yes you can.

        Makeshifts last the longest.

Re: substituting strings in HTML with text files
by DamnDirtyApe (Curate) on Jul 24, 2002 at 05:01 UTC

    Since you're here, I'll offer you a more Perl-ish solution: take a look at some of the templating packages for Perl, particularily HTML::Template and Template Toolkit. both of these can include external files in an HTML file.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: substituting strings in HTML with text files
by BUU (Prior) on Jul 24, 2002 at 04:58 UTC
    Uh, if the page is already being 'dynamically generated' (from the cgi script), why not just have it insert the text? HTML::Template might help.

    Note that he is correct, in that the output of a script is not parsed for ssi commands. You can however force that with the use of the the ssi-chain module somewhere on cpan