AtomicSoft.com Add power to your website!
Home Products Services Support Free
EasyData/SQL
easy-to-use web interface for SQL databases with countless features
AtomicDesk
help desk, FAQ, knowledge base, live support chat - all in one product
Custom programming
Installation
Documentation
HelpDesk
Libraries
Join our newsletter for information on software updates and new releases. You may unsubscribe at any time.



Click here to download plain text source code.

# subroutine name: process_template
# Input: template filename, reference to hash {tag_name=>tag_value}
# Processes <!--$tag_name--> tags and <!--%% "perl commands" %%--> tags
# inside template file. <!--$tag_name--> tags are replaced with corresponding values.
# Returns: string (result of tempate file processing).
# Global variables used:
#    %template_subst - hash for templates substitutions
#    %templates_cache - caching hash for small templates (<5000 bytes)
#    $templates_dir - filesystem path to templates dir (without trailing slash)
#   $admin - admin works with script (disable template substitution feature)
sub process_template {
    local ($templatefile, $namespace) = @_;
    my $template_content;
    if ($template_subst{$templatefile} ne "" && !$admin) {
        # replace filename, if this name found in template substitution hash
        $templatefile=$template_subst{$templatefile};
    }
    if ($templatefile=~m/^[\w\.\-\~]+$/s) {
        if (defined $templates_cache{$templatefile}) {
            $template_content = $templates_cache{$templatefile};
        } else {
            my $fn="$templates_dir/$templatefile";
            $| = 1;
            # Open template and read in
            open(TEMPLATE,"<$fn") or die("Error in subroutine process_template: can't open $fn. Reason: $!");
            {
                local($/) = undef;
                $template_content = <TEMPLATE>;
            }
            close(TEMPLATE);
            # cache template (useful for repeating template use within one CGI process)
            $template_content =~ s/^<!--(|[^\$\%].*?)-->//s; #remove infoheader
            $templates_cache{$templatefile}=$template_content if (length($template_content)<5000);
        }
        #$template_content =~ s/<!--\$([A-Za-z\-_\.\d]+)-->/$namespace->{$1}/gs;
        #$template_content =~ s/<\!--\%\%(.+?)\%\%-->/&evaluate($1)/egs;
        return &process_text($template_content, $namespace);
    } else {
        die("Error in subroutine process_template: template filename '$templatefile' may contain only alpha-numerical and '.', '-', '~', '_' characters.");
    }
}
 
Copyright © 1999-2007 Atomicsoft Ltd. All Rights Reserved.