CGI::okTemplate - Perl extension for easy creating websites with using templates


CGI-okTemplate documentation  | view source Contained in the CGI-okTemplate distribution.

Index


NAME

Top

CGI::okTemplate - Perl extension for easy creating websites with using templates

SYNOPSIS

Top

  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

Top

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} - 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 '%>'

	this tag includes file named {filename} into current
	template file.

	{filename} can be relative (to current template path) 
	or absolute path to the file

	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

Top

	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

Top

	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

	parses template with data sctructure $data

	$data is the hash reference (see the section DATA STRUCTURE)

DATA STRUCTURE

Top

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

Top

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

Top

Oleg Kobyakovskiy, <ok.perl &at; gmail &dot; com>

COPYRIGHT AND LICENSE

Top


CGI-okTemplate documentation  | view source Contained in the CGI-okTemplate distribution.