Category: template/text manipulation
Author/Contact Info maddfisherman/ anyone can use this code, but please tell me if they have a reason to change it so i can change it on my comp
Description: This is a templating program that uses two text files one that is like a db file and the other is a template file. The db file should look like this:
****filename.ext****HELLO\file1.txt****filename.ext**** ****stuff****THIS IS A SUBSTITUTION****stuff**** ****title****1****title**** ****filename.ext****HELLO\HELLO\file2.txt****filename.ext**** ****stuff****THIS IS A SUBSTITUTION #2 THAT KEEPS LINE BREAKS****stuff**** ****title****2****title****
the first part of every entry must be ****filename.ext****the filename and path you want****filename.ext**** and the other parts must be surrounded by ****anykeyword**** ...****thesamekeyword**** as long as that keyword has a corresponding keyword in the template file. The template file should look like this:
****stuff****and this should be second, and here is the title ****title****here are mor +e words hope it still works
you can have as many substitutions as you want and can have the keyword be what ever you want as long as they correspond between the two files. Also in the code you should edit where the 2 files exist and where you want the program to create the files. THANKS. please alert me to any bugs or advice. Hope this is helpfull for some. A few lines are based on the windows op sys so if your running unix please tell me how to change those few lines so that it works on all operating systems.
#!/usr/bin/perl -w
use Carp;
use strict;
use warnings;

my $template_name = 'C:\Perl\template\template1.html';
my $db_name = 'C:\Perl\template\text1.txt';
my $index = 'filename.ext';
my $start_dir = 'C:\Perl\template\website';
my @star_dir = split (/\\/, $start_dir);
foreach (@star_dir) {
    if ($_ eq 'C:') {$_ = 'C:\\'}
    unless (-e "$_") { mkdir ("$_", 0700) or die "can't mkdir $_: $!"}
    chdir ("$_") or die "can't chdir $_: $!";
}
my $i = 0; my $k = 0;
open (DB, "<$db_name") or die "can't open $db_name: $!";
PRINTING_LOOP: local $/ = "****$index****";
while (<DB>) {
    if ($i == 0) {
        $i = 1;
        next;
    }
    else {
        chomp;
        if ($index eq 'filename.ext') {
            my @dir = split (/\\/);
            foreach (@dir) {
                if ($_ eq "$dir[-1]") {
                    open (NEWFILE, ">$_") or die "can't open $_: $!";
                    foreach (@dir) {
                        if ($_ eq "$dir[-1]") {last}
                        else {chdir ("..") or die "can't chdir ..: $!"
+}
                    }
                }
                else {
                    unless (-e "$_") {mkdir ("$_", 0700) or die "can't
+ mkdir $_: $!"}
                    chdir ("$_") or die "can't chdir $_: $!";
                }
            }
            open (TEMPLATE, "<$template_name") or die "can't open $tem
+plate_name: $!";
        }
        else {
            print NEWFILE "$_";
            if ($k == 1) {
                $index = 'filename.ext';
                $k = 0; $i = 0;
                goto PRINTING_LOOP;
            }
        }
        local $/ = "****";
        my $j = 0;
        while (<TEMPLATE>) {
            chomp;

            if (eof(TEMPLATE)) {
                if ($j == 0){
                    print NEWFILE "$_";
                    close (NEWFILE);
                    close (TEMPLATE);
                    $i = 0;
                    $index = 'filename.ext';
                    goto PRINTING_LOOP;
                }
                else {
                    $index = "$_";
                    close (TEMPLATE);
                    $i = 0; $k = 1;
                    goto PRINTING_LOOP;
                }
            }
            else {
                if ($j == 0) {
                    print NEWFILE "$_";
                    $j = 1;
                }
                else {
                    $i = 0;
                    $index = "$_";    
                    goto PRINTING_LOOP;
                }
            }
        }
    }
}
close (DB);
Replies are listed 'Best First'.
Re: efficient template
by IOrdy (Friar) on Aug 30, 2001 at 06:37 UTC
    I started off doing stuff like this, my suggestion though is to check out HTML::Template on the cpan. It's a very easy module to use, but if you need more check out this node for a great tutorial.

    Keep on coding though :)