NAME
CGI::okTemplate - Perl extension for easy creating websites with using templates
SYNOPSIS
use CGI::okTemplate;
my $tmp = new CGI::okTemplate();
$tmp->read_template('t/test.tpl');
print $tmp->parse($data);
or
use CGI::okTemplate;
my $tmp = new CGI::okTemplate(File=>'t/test.tpl');
print $tmp->parse($data);
DESCRIPTION
This is an object oriented template module which parses template files and puts data instead of macros.
Template document can have 3 types of special TAGs:
- <% {macroname} %>
-
{macroname} - name of variable which will be changed to its value if {macroname} undefined in currect block it will be changed in outer block. if {macroname} undefined in outer block also then this tag will be erased from document if function 'new' not receives parameter 'NoClean' or leave as is if parameter 'NoClean' not used you can put spaces between '<%' and '{macroname}' and '%>'
- <!--Include {filename}-->
-
this tag includes file named {filename} into current template file. {filename} can be relative (to current template path) or absolute path to the file
- <!--{BlockTag} {blockname}-->...<!--/{BlockTag} {blockname}-->
-
this tag defines the start and end of the block {BlockTag} can be defined in function 'new' by parameter 'BlockTag' default {BlockTag} is 'TemplateBlock' {blockname} is the name of block.
FUNCTIONS
- new
-
creates new template object use CGI::okTemplate; my $tmp = new CGI::okTemplate(); Possible parameters: BlockTag - defines the key work for block tags File - path to template file (relative or absolute) RootDir - dir where all templates have to be under (default is current dir) NoClean - says to not clean tags for undefined variables Debug - says to put an info into parsed document
OBJECT FUNCTIONS
- read_template($filename)
-
defines template file named $filename to he template object this function will overwrite the template data defined in 'new' function if that function received 'File' parameter
- parse($data)
-
parses template with data sctructure $data $data is the hash reference (see the section DATA STRUCTURE)
DATA STRUCTURE
Data for template is the hash reference.
Hash can have 2 types of value for each key.
1) string value - it means that this element
has value for {macroname} of current (or inner) block;
2) array reference - it means that this element
has array of data for block named my key name;
Example :
Perl Code (example1.pl):
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#!/usr/bin/perl
use CGI::okTemplate;
my $tmpl = new CGI::Template( File=>'templates/example1.tpl',
BlockTag=>'Block');
$data = {
header => 'value for "header" macro',
footer => 'value for "footer" macro',
row => [
{value => 'value1',},
{value => 'value2',},
{value => 'value3',},
],
};
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Template File (templates/example1.tpl): -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- <%header%> <!--Block row--><%value%><!--/Block row--> <%footer%> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
The result have to be: -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- value for "header" macro value1 value2 value3 value for "footer" macro -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
As we can see row has reference to the array of simple data structures for block named 'row' This array has 3 elemens. So this block will be parsed 3 times.
See example folder for examples.
EXPORT
None by default.
SEE ALSO
examples/*.pl - for scripts
examples/templates/* - for templates
A mailing list for this module not opened yet.
A web site for this and other my modules is under construction.
AUTHOR
Oleg Kobyakovskiy, <ok.perl &at; gmail ˙ com>
COPYRIGHT AND LICENSE
Copyright (C) 2005 by Oleg Kobyakovskiy
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.
6 POD Errors
The following errors were encountered while parsing the POD:
- Around line 262:
'=item' outside of any '=over'
- Around line 292:
You forgot a '=back' before '=head1'
- Around line 294:
'=item' outside of any '=over'
- Around line 309:
You forgot a '=back' before '=head1'
- Around line 311:
'=item' outside of any '=over'
- Around line 323:
You forgot a '=back' before '=head1'