CTK::Template - yet another template engine
template.html:
<html> <head><title>Sample template 1</title> <body> My Home Directory is $home <p> My Path is set to $path </body> </html>
CGI program:
#!/usr/bin/perl -w
use CTK::Template;
my $t = CTK::Template->new(filename => 'template.html');
$t->param(home => $ENV{HOME});
$t->param(path => $ENV{PATH});
print "Content-Type: text/html\n\n";
print $t->output;
-or-
my $t = CTK::Template->new(path => [
'/var/templates/skel/',
'/var/templates/tmpl/',
],
filename => 'template.html');
$t->param(home => $ENV{HOME});
$t->param(path => $ENV{PATH});
print "Content-Type: text/html\n\n";
print $t->output;
head.html:
<html>
<head><title>Sample template 2</title>
<body>
$var{$var's Friends}{My Friends}:
<p>
loop.html:
$name ($age), $tel<br>
foot.html:
</body> </html>
CGI program:
#!/usr/bin/perl -w
use CTK::Template;
my $t = CTK::Template->new(head => 'head.html',
loop => 'loop.html',
foot => 'foot.html');
$t->param(var => 'Jon'); # or My Friends: $t->param(var => undef);
$t->param(loop => [
{ name => 'Ken', age => '20', tel => '00-1111-1111' },
{ name => 'Mike', age => '30', tel => '01-1111-1111' },
{ name => 'Bob', age => '40', tel => '02-1111-1111' },
]);
print "Content-Type: text/html\n\n";
print $t->output;
-or-
my $t = CTK::Template->new(head => 'head.html',
loop => 'loop.html',
foot => 'foot.html',
data => {
var => 'Jon', # or My Friends: var => undef,
loop => [
{ name => 'Ken',
age => '20',
tel => '00-1111-1111',
},
{ name => 'Mike',
age => '30',
tel => '01-1111-1111',
},
{ name => 'Bob',
age => '40',
tel => '02-1111-1111',
},
],
}
);
print "Content-Type: text/html\n\n";
print $t->output;
my $t = CTK::Template->new(path => [],
filename => undef,
head => undef,
loop => undef,
foot => undef,
data => {},
loop_filter => undef);
path: テンプレート検索パス(デフォルト [])
テンプレートの検索パスを配列のリファレンスで指定します。 最初に記述したディレクトリから順にテンプレートファイルを検索し、 最後は実行時のディレクトリ(.)で検索します。 テンプレートの検索に失敗した場合は、エラーになります。
filename: テンプレートファイル名(デフォルト undef)
filename は、1 つのテンプレートファイルで表示を完結できる場合に使用します。
繰り返しの表現が必要な場合は、3 つのテンプレートを head, loop,
foot で指定して下さい。
head: ヘッダーテンプレートのファイル名(デフォルト undef)
loop: ループテンプレートのファイル名(デフォルト undef)
foot: フッターテンプレートのファイル名(デフォルト undef)
data: テンプレート変数と値(デフォルト {})
テンプレートで表示する変数名と値をハッシュのリファレンスで指定します。
あとから param で設定することもできます。
data => {
title => 'Address Table',
loop => [
{ name => 'Ken', age => '20', tel => '00-1111-1111' },
{ name => 'Mike', age => '30', tel => '01-1111-1111' },
{ name => 'Bob', age => '40', tel => '02-1111-1111' },
],
}
loop_filter: ループフィルター(デフォルト undef)
繰り返し表現でフィルターを追加したい場合に指定します。
loop_filter => \&loop_filter()
sub loop_filter {
my ($rows, $datref) = @_;
$datref->{age3x} = 1 if $datref->{age} >= 30 && $datref->{age} < 40;
$datref->{tel} =~ tr/0-9//dc;
}
値の追加や変更を行うことができます。
$t->param(title => 'Address Table');
パラメータに値を設定します。
@keys = $t->param();
パラメータを指定しない場合は、すべてのパラメータ名を配列で返します。 繰り返し表示したい値は、loop パラメータに配列のハッシュで指定して下さい。
$t->param(loop => [
{ name => 'Ken', age => '20', tel => '00-1111-1111' },
{ name => 'Mike', age => '30', tel => '01-1111-1111' },
{ name => 'Bob', age => '40', tel => '02-1111-1111' },
]);
loop パラメータで指定した値は、繰り返し表示のみで表示されます。 loop 以外のパラメータは、どのテンプレートでも表示させることができます。
print $t->output;
テンプレートに値を埋め込んだ処理結果を返します。
テンプレートで使用するタグです。
Your name is $name!
パラメータ var が設定されていない場合や、var の値が空欄の場合は、$var が取り除かれて表示されます。
$name{Your name is $name!}
{}の中には、$var を記述することができます。
Perl のコードで表現すると以下と同等の処理が行われます。
if ($name) {
print "Your name is $name!";
}
$name{Your name is $name!}{What's your name?}
{}の中には、$var を記述することができます。
Perl のコードで表現すると以下と同等の処理が行われます。
if ($name) {
print "Your name is $name!";
} else {
print "What's your name?";
}
例を示します。
head.html:
<html> <head><title>$title</title> <body> <table> <tr><td>Name</td><td>Age</td><td>Tel</td><td>Name</td><td>Age</td><td>Tel</td></tr> <tr>
loop.html:
<tr> <td>$name</td><td>$age</td><td>$tel</td> <!--__SNIP__--> <td>$name</td><td>$age</td><td>$tel</td> </tr>
foot.html:
</tr> </table> </body> </html>
表示結果
Name Age Tel | Name Age Tel ----------------------+----------------------- Ken 20 00-1111-1111 | Mike 30 01-1111-1111 Bob 40 02-1111-1111 |
Yutaka Kojima <yutaka@asmate.net>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.