HandyKit v2.1  

 Online
 Index page
 HowTo

HandyKIT packages

 HandyCGI.pm
 HandyList.pm
 HandyMail.pm
 HandyTemplate.pm
 HandyTextDB.pm
 SiteBag.pm

Other packages

 HandySQL.pm
 HandyLog.pm
 HandyCrypt.pm

NAME

HandyTemplate - full-featured module to work with template files.


SYNOPSIS

Simple call:

my $html = &load_template('simple_one.html');

Simple call to load non-local template:

my $html = 
  &load_template('http://someserver/remote_template.html');

or even:

my $html = 
  &load_template('http://someserver/script.cgi?id=12345');

Putting some values into template file:

%tvars = ();
$tvars{'time'} = time;
my $html = &load_template('time.html',{'DONTINITTVARS' => 1});

time.html, for example contains:

<html><head><title>Time Test</title></head>

<body>Current time is: &time;</body></html>

Defining own procedure handlers (part of some example script like "credit_card_process.cgi"):

my $fp = { 
  'main' => sub { 
    $tvars{'cardnumber'} = $hin{'cardnumber'}; 
    if (length($hin{'cardnumber'}) != 16) {
      $tvars{'noerrcardnumber'} = $tvars{'errcardnumber'}; 
    } return $_[0]; 
  } 
};

my $html= &load_template('card.html',$fp);

card.html, for example contains:

<html>
<head>
<title>Credit card number length validator</title>
</head>

<body>

<form action="credit_card_process.cgi" method="post">

&noerrcardnumber;<input type="text" name="cardnumber"
value="&cardnumber;" size="16">

<input type="submit" name="go" value=" GO ">

</form>
</body>
</html>

&vars{noerrcardnumber}{Please, enter card number:}
{errcardnumber}{Incorrect length! Please, reenter card number:}


CONFIGURATION

config.pm

We encourage you to store all your project configuration data in config module inside of hash %cfg. All configuration for HandyCGI, HandyTemplate, HandySQL, HandyLog, HandyMail and HandyList modules are stored in this file by default. However, if you dislike this approach feel free to modify code a little.

tmpldebug
If 1 then all debug information will be written to logs. 0 - no debug messages in log.

Default: 0

template_path
Path to template files.

Default: templates/


DESCRIPTION

Module exports one function &load_template and hash of template variables %tvars.

&load_template can be called with one or two arguments. The first one is the name of template (or URL of template), it is mandatory. The second one is the reference to hash, that contains references template functions. It is called formprocs hash.

%tvars is a hash, that contain template variables. Entities with names equal to keys located of hash %tvars are replaced by their values. e.g. &something; is replaced to $tvars{'something'}.


Template functions

When in template file &function{arg0}{arg1} arrives, then formprocs hash is looked for key function and corresponding subroutine is launched with arguments arg0, arg1, etc. Number of arguments is limited only by available resources.

NOTE:

If you do not want to disrupt HTML format, you can write:
<!-- &function{ --> arg0 <!-- }{ --> arg1 <!-- } -->

Please, note, that between function name and '{' or '}{' there is NO SPACE.


Braces in templates

You should use braces '{' and '}' only in template functions. In all other places use &lbr; for '{' replacement and &rbr; for '}' replacement.


BUILT-IN TEMPLATE FUNCTIONS

main{body of template}
This function often should be redefined. It is the topmost one. It is called just before template variable substitution and after all other template functions are called. Default one just return its input intact.

vars{variable name}{value}
Equivalent to perl $tvars{'variable name'} = 'value'; Supports multiple arguments, so $tvars{'a1'} = 'b1'; $tvars{'a2'} = 'b2'; could be written as:

&vars{a1}{b1}{a2}{b2}

vars_include{template filename}
Parses template file, reading only &vars{} template function calls from there.

if_hin{key}{value}{if matches}{else}
If $hin{'key'} eq 'value', then 'if matches' is returned. Otherwise 'else' is returned.

if_hl{%hl key}{value}{if matches}{else}
If $hl{'key'} eq 'value', then 'if matches' is returned. Otherwise 'else' is returned.

if_cookie{cookie key}{value}{if matches}{else}
If &get_cookie('key') eq 'value', then 'if matches' is returned. Otherwise 'else' is returned.

escape{string to escape}
Returns URL-encoded string.

unescape{string to unescape}
Decodes URL-encoded string.

format_date{time}
time is time in format as returned by time() perl runtime function. If time specified is within current day then formatted string is HH:MM:SS - which represents time. Otherwise in DD-MM-YYYY date format.

format{format string}{arg1}{arg2}...{argN}
Equivalent of sprintf('format string','arg1','arg2'...,'argN');

eval{perl code}
Executes perl code and returned value is inserted into template file. We encourage you to use this function rarely, and define your template functions instead.



TOP
© 2000 || Perl4YOU.com E-mail: info@perl4you.com