|
Class::Skin - Class for creating text out of text templates. |
Class::Skin - Class for creating text out of text templates.
use Class::Skin;
# create new Class::Skin object
my $skin = new Class::Skin("template.txt");
# read the template file $skin->read();
# parse the template and print the result. we send in anonymous
# hash all the variables that we would like to use in the template
print $skin->parse({ title => $title,
i => $i,
condition1 => 1,
condition2 => 0,
condition3 => 1,
condition4 => 0,
condition5 => 1,
condition6 => 0,
condition7 => 1,
condition8 => 0,
condition9 => 1,
filepath => "template2.txt",
func => \&call_back_func});
The elements that can be used in the templates:
<include src="other_template.txt">
<include src="$name">
<include src="template.txt" skip="2">
The included template path can be absolute, or relative. If it is relative, it will be searched, relative to the host template path, or relative to the list of directories. This list is populated by default by the path of the current directory and the path of the hosted templates. It can be manipulated by the access method directory_list.
read()
Here is template1.txt:
<comment> this comment will not be shown in the final html </comment> <comment> the title will be in "title"</comment> *** $title ***
Here two templates will be included: <comment> including template with its filepath </comment> <include src="template2.txt">
<comment> including template that its path is in a variable called filepath </comment> <include src="$filepath">
Here we will put some conditions:
<if condition="condition1">
condition1 is true
<if condition="condition2">
condition2 is true (in condition1)
</if>
<if condition="condition3">
condition3 is true (in condition1)
<if condition="condition5">
condition5 is true (in condition3 that in condition1)
</if>
<else>
condition5 is false (in condition3 that in condition1)
</else>
</if>
</if>
<elsif condition="condition2">
condition2 is true
</elsif>
<if condition="condition4">
condition4 is true
</if>
<elsif condition="condition5">
condition5 is true
</elsif>
<elsif condition="condition6">
condition6 is true
</elsif>
<else>
condition5,4,6 are false
</else>
Here we will put a table where the rows are in a while loop. Note that we use $a here, but $a is not defined in the calling script, so it stays $a. Only in the last loop of the while we will define $a. <while condition="func"> $i: $a <if condition="condition7"> condition7 is true inside the while loop. </if><else> condition7 is false inside the while loop. </else> </while>
And template2.txt:
--- start of template2 --- <comment> this is a comment in the included template </comment> included template 2 is here. Template 2 is inserted into the first template. We can put use here all the variables like "$title". --- end of template2 ---
The script:
use Class::Skin;
my $i = 777; my $title = "Usage of Class::Skin";
# create new Class::Skin object
my $skin = new Class::Skin("template1.txt");
# read the template file $skin->read();
# parse the template and print the result. we send in anonymous
# hash all the variables that we would like to use in the template
print $skin->parse({ title => $title,
i => $i,
condition1 => 1,
condition2 => 0,
condition3 => 1,
condition4 => 0,
condition5 => 1,
condition6 => 0,
condition7 => 1,
condition8 => 0,
condition9 => 1,
filepath => "template2.txt",
func => \&call_back_func});
# callback function. we will give a reference to that function as
# one of the variables we sent with the parse method. callback
# functions are used with the 'while' block. this function must
# return true or false (1 or 0). it is important that it will not
# return always true (or else the while block will be repeated
# infinite times).
sub call_back_func {
my $variables = shift; # always a reference to the anonymous hash
# that we send with the parse method is
# send to the callback functions.
$variables->{i}++; # increment the value of i
# toggle the value of condition9 according to the value of i
if ($variables->{i} == 779) {
$variables->{condition9} = 1;
}
else {
$variables->{condition9} = 0;
}
if ($variables->{i} == 780) {
$variables->{a} = "[this is a]";
}
else {
$variables->{a} = undef;
}
# return true unless i > 780.
if ($variables->{i} <= 780) {
return 1;
}
else {
return 0;
}
} # of call_back_func
The result:
*** Usage of Class::Skin ***
Here two templates will be included:
--- start of template2 ---
included template 2 is here. Template 2 is inserted into the first template. We can put use here all the variables like "Usage of Class::Skin". --- end of template2 --- --- start of template2 ---
included template 2 is here. Template 2 is inserted into the first template. We can put use here all the variables like "Usage of Class::Skin". --- end of template2 ---
Here we will put some conditions:
condition1 is true
condition3 is true (in condition1)
condition5 is true (in condition3 that in condition1)
condition5 is true
Here we will put a table where the rows are in a while loop.
Note that we use $a here, but $a is not defined in the calling script,
so it stays $a. Only in the last loop of the while we will define $a.
778: $a condition7 is true inside the while loop.
779: $a condition7 is true inside the while loop.
780: [this is a] condition7 is true inside the while loop.
Rani Pinchuk, rani@cpan.org
Copyright (c) 2002 Ockham Technology N.V. & Rani Pinchuk. All rights reserved. This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
the Log::NullLogLite(3) manpage, the Log::LogLite(3) manpage
|
Class::Skin - Class for creating text out of text templates. |