#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    Lorain, OH, USA
    Posts
    0
    Rep Power
    0
    I was given a code by the kind Kent Boortz, but I am very confused with it. My original question was:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Including HTML into CGI then printing out the answers:

    I have a simple CGI that displays HTML (that is inside the code itself) with a string simular to:
    print <<EOF;

    <html><head><title>!name!: Configure Your Preferences</title></head>
    <center>

    EOF

    Well, editing this CGI for my HTML templates does not seem fruitful. I would rather have the CGI pull the HTML into it and then edit the fields (see !name! above) and print the completed part out.
    [/quote]

    Kent's reply with code was:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    I use templates for all my scripts. Like the example

    my %data = ( 'name' => 'kent' );
    subst_write_template(%data, "/path/to/template");

    where

    sub subst_write_template($) {
    my $query = shift;
    my $template = shift;

    open(TEMPLATE,$template)
    &#0124; &#0124; die "Can't open "$template": $!n";

    while(<TEMPLATE> ) {
    s/${([A-Za-z][w-]*)}/$query->{$1}/g;
    s/$([A-Za-z][w-]*)/$query->{$1}/g;
    print;
    }

    close TEMPLATE;
    }

    and the template look like

    <html>
    .
    .
    .
    $name
    .
    </html>
    [/code]

    There are 2 problems.

    (1) I have NEVER seen a 'where' used before.

    (2) I cannot figure out how to put this into a Perl script.

    Can someone help me please?

    Thanks,

    Chris
  2. #2
  3. Ole` Timer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    N.W. Iowa
    Posts
    472
    Rep Power
    14
    I think the "where" is just a comment that wa not commented out in the code. If your using this code in your script, the routine: subst_write_template calls the sub to produce the template for display and the %data is passed to the template as an referenced hash.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>

    my %data = ( 'name' => 'kent' );
    subst_write_template(%data, "/path/to/template");
    # COMMENT => where
    sub subst_write_template($) {
    my $query = shift;
    my $template = shift;
    open(TEMPLATE,$template) &#0124; &#0124; die "Can't open "$template": $!n";
    while(<TEMPLATE> ) {
    s/${([A-Za-z][w-]*)}/$query->{$1}/g;
    s/$([A-Za-z][w-]*)/$query->{$1}/g;
    print;
    }
    close TEMPLATE;
    }
    [/code]

    ------------------
    <A HREF="http://www.thunder-rain.com" TARGET=_blank>
    Thunder Rain Internet Publishing</A>
    Providing Personal/Business
    Internet Solutions that work!

IMN logo majestic logo threadwatch logo seochat tools logo