NAME
Text::Yats - Yet Another Templete System
SYNOPSIS
use Text::Yats;
my $template = <<ENDHTML;
<html>
<head>
<title>\$title - \$version</title>
</head>
<body>
<form>
<select name="names"><!--{1}-->
<option \$selected>\$list</option>
<!--{2}--></select>
</form>
</body>
</html>
ENDHTML
my $result = "";
my $tpl = Text::Yats->new(
level => 1,
text => $template);
$result .= $tpl->section->[0]->replace(
title => "Yats",
version => "Development", );
$result .= $tpl->section->[1]->replace(
list => ['hdias','anita','cubitos'],
selected => { value => "selected",
array => "list",
match => "anita", } );
$result .= $tpl->section->[2]->text;
print $result;
-- Result --
<html> <head> <title>Yats - Development</title> </head> <body> <form> <select name="names"> <option>hdias</option> <option selected>anita</option> <option>cubitos</option> </select> </form> </body>
DESCRIPTION
This is yet another module for template-based text generation. You can store a template in a file outside your program. People can modify the template without modifying the program.
CONSTRUCTORS
use Text::Yats;
my $tpl = Text::Yats->new(
level => 1,
file => "templates/test.txt");
METHODS
new()
This method create a new Template object. You must call new() with at least one key => value pair.
- file
-
This is the name of the template to be loaded. It can be an absolute or relative pathname.
- text
-
This creates a template from in-memory text data
- level
-
If the block "<--{x}-->" is found, the template have one or more sections. "x" is a number or numbers and points.
level 0 (only text no sections, default option)
text1
$tpl->text(); this return text1
level 1 (text with separator of level 1)
text1<!--{1}-->text2<!--{2}-->text3
$tpl->section->[0]->text(); this return text1
$tpl->section->[1]->text(); this return text2
$tpl->section->[2]->text(); this return text3
level 2 (text with separator of level 2)
text1<!--{1}-->text2<!--{1.1}-->text3<!--{2}-->text4
$tpl->section->[0]->text(); this return text1
$tpl->section->[1]->section->[0]->text(); this return text2
$tpl->section->[1]->section->[1]->text(); this return text3
$tpl->section->[2]->text(); this return text4
level n (n is a number)
section()
The section is a array ref that contains all the parts of the templates and sub templates (template->section->[n]).
replace()
This method substitutes all the variables which are defined in the template ($var) with producing output. You can provide substitution parameters in three forms (you can mix them if you want):
- array reference
-
var_a => ["a","b","c","d","e"],
- scalar value
-
var_b => "e",
- hash reference
-
var_c => { value => "selected", array => "var_a", match => "c", }
Replace var_c with "selected" when it finds "c"
var_c => { value => "selected", array => "var_a", match => ["b","d"], }
Replace var_c with "selected" when it finds "b" or "d"
text()
This method return only the text, not replace anything.
AUTHOR
Henrique Dias <hdias@esb.ucp.pt>
VERSION
version 0.03
SEE ALSO
perl(1).