)))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
HTML-Template-Compiled-1.001/examples/objects.html 000444 001750 001750 5243 12141164036 21127 0 ustar 00tina tina 000000 000000 HTC example with objects
Script: examples/objects.pl
Found 4 persons:
Name | Age | Hair |
Simpson, Bart |
10 |
yellow |
Simpson, Maggie |
10 |
yellow |
Simpson, March |
42 |
purple |
Simpson, Homer |
42 |
none |
The Script:
#!/usr/bin/perl
package HTC::Object;
use strict;
use warnings;
use base qw(Class::Accessor);
__PACKAGE__->follow_best_practice;
__PACKAGE__->mk_accessors(qw(first last age));
sub fullname {
my $first = $_[0]->get_first;
my $last = $_[0]->get_last;
return "$last, $first";
}
package main;
use strict;
use warnings;
use HTML::Template::Compiled;
use Fcntl qw(:seek);
my ($template, $perlcode);
{
local $/;
$template = <DATA>;
seek DATA, 0, SEEK_SET;
$perlcode = <DATA>;
}
my $htc = HTML::Template::Compiled->new(
scalarref => \$template,
tagstyle => [qw(+tt)],
use_expressions => 1,
);
my $persons = [
HTC::Object->new({first => 'Bart', last => 'Simpson', age => 10, hair => 'yellow'}),
HTC::Object->new({first => 'Maggie', last => 'Simpson', age => 10, hair => 'yellow'}),
HTC::Object->new({first => 'March', last => 'Simpson', age => 42, hair => 'purple'}),
HTC::Object->new({first => 'Homer', last => 'Simpson', age => 42, hair => 'none'}),
];
$htc->param(
count => scalar @$persons,
items => $persons,
script => $0,
perlcode => $perlcode,
columns => [qw/ age hair /],
);
my $output = $htc->output;
print $output;
__DATA__
<html><head><title>HTC example with objects</title></head>
<body>
<h2>Script: [%= .script %]</h2><p>
Found [%= .count %] persons:
<table>
<tr><th>Name</th>[%loop .columns %]<th>[%= expr="ucfirst(_)" %]</th><%/loop %></tr>
[%loop items alias=person %]
<tr>
<td>[%= fullname %]</td>
[%loop .columns alias=column PRE_CHOMP=3 %]
<td>[%= expr="person{column}" %]</td>
[%/loop PRE_CHOMP=3 %]
</tr>
[%/loop items%]
</table>
<hr>
<h2>The Script:</h2>
<pre>
[%= perlcode escape=html %]
</pre>
</body></html>
HTML-Template-Compiled-1.001/examples/test.htc.20 000444 001750 001750 42350 12141164036 20527 0 ustar 00tina tina 000000 000000 ===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
HTML-Template-Compiled-1.001/examples/test.tt 000444 001750 001750 1553 12141164036 20140 0 ustar 00tina tina 000000 000000 ===test.html 1==========================================
name: [% name %] look ma: ~
name with "": [% name %]
INCLUDE: ((([% INCLUDE included.tt %])))
---------------
loop a:
[% FOREACH var = loopa %]first?[% loop.first() %] or last?[% loop.last() %]
-----num:[% loop.index() %]
item: [% var.a %]
[% END %]
loop b:[% FOREACH var = loopb %]item: ROOT:[% (!loop.first && !loop.last)%][% END %]
loop c
---------------
[% FOREACH var = c %]----num:[% loop.index() %]
[% FOREACH var = d %]*[% IF loop.first() %]first[% END %][% IF loop.last() %]last [% END %][% IF (!loop.first && !loop.last) %]inner[% END %] item: [% F %][% IF (loop.index % 2) %]odd[% END %]
[% END %][% END %]
---------------------
[% IF if2 %]if.if2![% END %] [% IF if3 %]if.if3! [% ELSE %]no if.if3![% END %] [% UNLESS if3 %]no if.if3!![% END %]
===test.html ende==========================================
HTML-Template-Compiled-1.001/examples/bench.pl 000444 001750 001750 41112 12141164036 20237 0 ustar 00tina tina 000000 000000 #!/usr/bin/perl
# $Id: bench.pl 1126 2011-10-31 19:56:35Z tinita $
use strict;
use warnings;
use Getopt::Long;
use Benchmark qw/ timethese cmpthese /;
use FindBin qw/ $RealBin /;
chdir "$RealBin/..";
#use Devel::Size qw(size total_size);
my $count = 0;
mkdir "cache";
mkdir "cache/htc";
mkdir "cache/htcc";
mkdir "cache/hte";
mkdir "cache/htpl";
mkdir "cache/jit";
my %use = (
'HTML::Template' => 0,
'HTML::Template::Pro' => 0,
'HTML::Template::Compiled' => 0,
'HTML::Template::Pluggable' => 0,
'HTML::Template::Expr' => 0,
'HTML::Template::Compiled::Classic' => 0,
# 'HTML::Template::JIT' => 0,
'Template' => 0,
'Template::Alloy' => 0,
'Template::HTML' => 0,
'Template::AutoFilter' => 0,
'Template::Like' => 0,
'CGI::Ex::Template' => 0,
# not yet
'Text::ScriptTemplate' => 0,
'Text::Xslate' => 0,
);
for my $key (sort keys %use) {
eval "require $key";
$use{$key} = 1 unless $@;
my $version = $use{$key} ? $key->VERSION : "-";
printf "using %35s %s\n", $key, $version;
}
HTML::Template::Compiled->clear_filecache("cache/htc");
HTML::Template::Compiled->clear_filecache("cache/htcc");
my $debug = 0;
$ENV{'HTML_TEMPLATE_ROOT'} = "examples";
my $FILE_CACHE = 0;
my $MEM_CACHE = 1;
my $LOOP_CONTEXT = 1;
my $GLOBAL_VARS = 0;
my $CASE_SENSITIVE = 1;
my $default_escape = 0;
my $template_size = 1;
my $bench = 'timethese';
GetOptions(
"file-cache=i" => \$FILE_CACHE,
"mem-cache=i" => \$MEM_CACHE,
"loop-context=i" => \$LOOP_CONTEXT,
"global-vars=i" => \$GLOBAL_VARS,
"case-sensitive=i" => \$CASE_SENSITIVE,
"default-escape=i" => \$default_escape,
"template-size=i" => \$template_size,
"bench=s" => \$bench,
);
my $iterations = shift;
my $ht_file = 'test.htc';
my $htcc_file = $ht_file . 'c';
my $tt_file = "test.tt";
my $tst_file = "test.tst";
my $xslfile = "test.xslate";
$template_size =~ tr/0-9//cd;
if ($template_size > 1) {
for my $file ($ht_file, $htcc_file, $tt_file, $tst_file, $xslfile) {
open my $fh, "<", "examples/$file" or die "examples/$file: $!";
my $data = do { local $/; <$fh> };
my $new_file = "$file.n$template_size";
open my $out, ">", "examples/$new_file" or die $!;
print $out $data x $template_size;
$file = $new_file;
}
}
print "running with:
--file-cache $FILE_CACHE --mem-cache $MEM_CACHE --loop-context $LOOP_CONTEXT "
. "--global-vars $GLOBAL_VARS --case-sensitive $CASE_SENSITIVE "
. "--default-escape $default_escape --template-size=$template_size
";
my $STDOUT = 0;
sub new_htc {
my $t1 = HTML::Template::Compiled->new_file( $ht_file,
#path => 'examples',
case_sensitive => $CASE_SENSITIVE, # slow down
loop_context_vars => $LOOP_CONTEXT,
$default_escape ? (default_escape => 'HTML') : (),
debug => $debug,
# note that you have to create the cachedir
# first, otherwise it will run without cache
# cache_dir => ($FILE_CACHE ? "cache/htc" : undef),
file_cache_dir => ($FILE_CACHE ? "cache/htc" : undef),
file_cache => ($FILE_CACHE ? 1 : undef),
cache => $MEM_CACHE,
out_fh => $STDOUT ? 1 : 0,
global_vars => $GLOBAL_VARS,
tagstyle => [qw(-asp -comment)],
expire_time => 1000,
);
return $t1;
}
sub new_htcc {
my $t1 = HTML::Template::Compiled::Classic->new_file( $htcc_file,
#path => 'examples',
case_sensitive => $CASE_SENSITIVE, # slow down
loop_context_vars => $LOOP_CONTEXT,
$default_escape ? (default_escape => 'HTML') : (),
debug => $debug,
# note that you have to create the cachedir
# first, otherwise it will run without cache
cache_dir => ($FILE_CACHE ? "cache/htcc" : undef),
cache => $MEM_CACHE,
out_fh => $STDOUT ? 1 : 0,
global_vars => $GLOBAL_VARS,
debug => 0,
tagstyle => [qw(-asp -comment)],
expire_time => 1000,
#debug => 1,
);
return $t1;
}
sub new_tst {
my $t = Text::ScriptTemplate->new();
$t->load("examples/$tst_file");
#my $size = total_size($t1);
#print "size htc = $size\n";
return $t;
}
sub new_htp {
my $t2 = HTML::Template::Pro->new(
case_sensitive => $CASE_SENSITIVE,
loop_context_vars => $LOOP_CONTEXT,
$default_escape ? (default_escape => 'HTML') : (),
#path => 'examples',
filename => $ht_file,
# cache => $MEM_CACHE,
# $FILE_CACHE ?
# (file_cache => $FILE_CACHE,
# file_cache_dir => 'cache/ht') : (),
global_vars => $GLOBAL_VARS,
# die_on_bad_params => 0,
);
return $t2;
}
sub new_ht {
my $t2 = HTML::Template->new(
case_sensitive => $CASE_SENSITIVE,
loop_context_vars => $LOOP_CONTEXT,
$default_escape ? (default_escape => 'HTML') : (),
#path => 'examples',
filename => $ht_file,
cache => $MEM_CACHE,
$FILE_CACHE ?
(file_cache => $FILE_CACHE,
file_cache_dir => 'cache/ht') : (),
global_vars => $GLOBAL_VARS,
die_on_bad_params => 0,
blind_cache => 1,
);
return $t2;
}
sub new_hte {
my $t2 = HTML::Template::Expr->new(
case_sensitive => $CASE_SENSITIVE,
loop_context_vars => $LOOP_CONTEXT,
$default_escape ? (default_escape => 'HTML') : (),
#path => 'examples',
filename => $ht_file,
cache => $MEM_CACHE,
$FILE_CACHE ?
(file_cache => $FILE_CACHE,
file_cache_dir => 'cache/hte') : (),
global_vars => $GLOBAL_VARS,
die_on_bad_params => 0,
);
return $t2;
}
sub new_htpl {
my $t2 = HTML::Template::Pluggable->new(
case_sensitive => $CASE_SENSITIVE,
loop_context_vars => $LOOP_CONTEXT,
$default_escape ? (default_escape => 'HTML') : (),
#path => 'examples',
filename => $ht_file,
cache => $MEM_CACHE,
$FILE_CACHE ?
(file_cache => $FILE_CACHE,
file_cache_dir => 'cache/htpl') : (),
global_vars => $GLOBAL_VARS,
die_on_bad_params => 0,
);
return $t2;
}
sub new_htj {
my $t2 = HTML::Template::JIT->new(
loop_context_vars => 1,
$default_escape ? (default_escape => 'HTML') : (),
#path => 'examples',
filename => $ht_file,
cache => 1,
jit_path => 'cache/jit',
#global_vars => 1,
);
return $t2;
}
sub new_tl {
my $tt= Template::Like->new(
);
# $FILE_CACHE
# ? (
# COMPILE_EXT => '.ttc',
# COMPILE_DIR => 'cache/tt',
# )
# : (),
# $MEM_CACHE
# ? ()
# : (CACHE_SIZE => 0),
# INCLUDE_PATH => 'examples',
#my $size = total_size($tt);
#print "size tt = $size\n";
return $tt;
}
sub new_tt {
my $tt= Template->new(
$FILE_CACHE
? (
COMPILE_EXT => '.ttc',
COMPILE_DIR => 'cache/tt',
)
: (),
$MEM_CACHE
? ()
: (CACHE_SIZE => 0),
INCLUDE_PATH => 'examples',
);
#my $size = total_size($tt);
#print "size tt = $size\n";
return $tt;
}
sub new_ta {
my $tt = Template::Alloy->new(
$FILE_CACHE
? (
COMPILE_EXT => '.ttc',
COMPILE_DIR => 'cache/tt',
)
: (),
$MEM_CACHE
? ()
: (CACHE_SIZE => 0),
INCLUDE_PATH => 'examples',
);
#my $size = total_size($tt);
#print "size tt = $size\n";
return $tt;
}
sub new_ttaf {
my $tt= Template::AutoFilter->new(
$FILE_CACHE
? (
COMPILE_EXT => '.ttc',
COMPILE_DIR => 'cache/tt',
)
: (),
$MEM_CACHE
? ()
: (CACHE_SIZE => 0),
INCLUDE_PATH => 'examples',
);
#my $size = total_size($tt);
#print "size tt = $size\n";
return $tt;
}
sub new_tth {
my $tt= Template::HTML->new(
$FILE_CACHE
? (
COMPILE_EXT => '.ttc',
COMPILE_DIR => 'cache/tt',
)
: (),
$MEM_CACHE
? ()
: (CACHE_SIZE => 0),
INCLUDE_PATH => 'examples',
);
#my $size = total_size($tt);
#print "size tt = $size\n";
return $tt;
}
sub new_xslate {
# Text::Xslate has no file caching
my $t = Text::Xslate->new(
$MEM_CACHE ? (cache => 2, cache_dir => "cache/xslate") : (),
path => 'examples',
syntax => 'TTerse',
);
return $t;
}
sub new_cet {
my $tt= CGI::Ex::Template->new(
$FILE_CACHE
? (
COMPILE_EXT => '.ttc',
COMPILE_DIR => 'cache/tt',
)
: (),
$MEM_CACHE
? ()
: (CACHE_SIZE => 0),
INCLUDE_PATH => 'examples',
);
#my $size = total_size($tt);
#print "size tt = $size\n";
return $tt;
}
sub new_st {
my $st = Text::ScriptTemplate->new;
$st->load("examples/template.st");
}
my %params = (
name => '',
loopa => [{a=>3},{a=>4},{a=>5}],
#a => [qw(b c d)],
loopb => [{ inner => 23 }],
c => [
{
d=>[({F=>11},{F=>22}, {F=>33})]
},
{
d=>[({F=>44}, {F=>55}, {F=>66})]
}
],
if2 => 1,
if3 => 0,
blubber => "html ",
);
open OUT, ">>/dev/null";
#open OUT, ">&STDOUT";
sub output {
my $t = shift;
return unless defined $t;
$t->param(%params);
if ($STDOUT) {
my $out;
if ($t=~m/Compiled/) {
$out = $t->output(\*OUT);
}
else {
$out = $t->output;
}
print OUT $out;
}
else {
my $out = $t->output();
}
#print "output():$out\n";
#my $size = total_size($t);
#print "size $t = $size\n";
}
sub output_tst {
my $t = shift;
return unless defined $t;
$t->setq(%params,tmpl=>$t);
my $out = $t->fill;
#print "output_tst():$out\n";
if ($STDOUT) {
print OUT $out;
}
}
sub output_tl {
my $t = shift;
return unless defined $t;
chdir 'examples';
my $filett = $tt_file;
if ($STDOUT) {
$t->process($filett, \%params, \*OUT) or die $t->error();
}
else {
my $out;
$t->process($filett, \%params, \$out) or die $t->error();
}
#my $size = total_size($t);
#print "size $t = $size\n";
#my $out = $t->output;
#print "\nOUT: $out";
chdir '..';
}
sub output_tt {
my $t = shift;
return unless defined $t;
my $filett = $tt_file;
#$t->process($filett, \%params, \*OUT);
if ($STDOUT) {
$t->process($filett, \%params, \*OUT) or die $t->error();
}
else {
my $out;
$t->process($filett, \%params, \$out) or die $t->error();
}
#my $size = total_size($t);
#print "size $t = $size\n";
#print $t->{code} if exists $t->{code};
#my $out = $t->output;
#print "\nOUT: $out";
}
sub output_ttaf {
my $t = shift;
return unless defined $t;
my $filett = $tt_file;
#$t->process($filett, \%params, \*OUT);
if ($STDOUT) {
$t->process($filett, \%params, \*OUT) or die $t->error();
}
else {
my $out;
$t->process($filett, \%params, \$out) or die $t->error();
}
#my $size = total_size($t);
#print "size $t = $size\n";
#print $t->{code} if exists $t->{code};
#my $out = $t->output;
#print "\nOUT: $out";
}
sub output_xslate {
my $t = shift;
return unless defined $t;
my $filett = $tt_file;
#$t->process($filett, \%params, \*OUT);
# $t->process($filett, \%params, \*OUT) or die $t->error();
#my $size = total_size($t);
#print "size $t = $size\n";
#print $t->{code} if exists $t->{code};
my $out = $t->render($xslfile, \%params);
#my $out = $t->output;
if ($STDOUT) {
print OUT $out;
}
}
my $global_htc = $use{'HTML::Template::Compiled'} ? new_htc : undef;
my $global_htcc = $use{'HTML::Template::Compiled::Classic'} ? new_htcc : undef;
my $global_ht = $use{'HTML::Template'} ? new_ht : undef;
my $global_htp = $use{'HTML::Template::Pro'} ? new_htp : undef;
my $global_htpl = $use{'HTML::Template::Pluggable'} ? new_htpl : undef;
my $global_htj = $use{'HTML::Template::JIT'} ? new_htj : undef;
my $global_tt = $use{'Template'} ? new_tt : undef;
my $global_ta = $use{'Template::Alloy'} ? new_ta : undef;
my $global_ttaf = $use{'Template::AutoFilter'} ? new_ttaf : undef;
my $global_tth = $use{'Template::HTML'} ? new_tth : undef;
my $global_xslate = $use{'Text::Xslate'} ? new_xslate : undef;
my $global_tl = $use{'Template::Like'} ? new_tl : undef;
my $global_cet = $use{'CGI::Ex::Template'} ? new_cet : undef;
my $global_tst = $use{'Text::ScriptTemplate'} ? new_tst : undef;
if(1) {
my %args = (
$use{'HTML::Template::Compiled'} ? (
# deactivate memory cache
#new_htc_w_clear_cache => sub {my $t = new_htc();$t->clear_cache},
# normal, with memory cache
# new_htc => sub {my $t = new_htc()},
#output_htc => sub {output($global_htc)},
all_htc => sub {my $t = new_htc();output($t)},
) : (),
$use{'HTML::Template::Compiled::Classic'} ? (
# deactivate memory cache
#new_htc_w_clear_cache => sub {my $t = new_htc();$t->clear_cache},
# normal, with memory cache
# new_htcc => sub {my $t = new_htcc()},
#output_htc => sub {output($global_htc)},
all_htcc => sub {my $t = new_htcc();output($t)},
) : (),
$use{'HTML::Template'} ? (
# new_ht => sub {my $t = new_ht()},
#output_ht => sub {output($global_ht)},
all_ht => sub {my $t = new_ht();output($t)},
) : (),
$use{'HTML::Template::Pro'} ? (
# new_htp => sub {my $t = new_htpl()},
#output_htp => sub {output($global_htp)},
all_htp => sub {my $t = new_htp();output($t)},
) : (),
$use{'HTML::Template::Pluggable'} ? (
# new_htpl => sub {my $t = new_htpl()},
#output_htpl => sub {output($global_htpl)},
all_htpl => sub {my $t = new_htpl();output($t)},
) : (),
$use{'HTML::Template::Expr'} && !$FILE_CACHE ? (
# new_hte => sub {my $t = new_hte()},
#output_hte => sub {output($global_hte)},
all_hte => sub {my $t = new_hte();output($t)},
) : (),
$use{'HTML::Template::JIT'} ? (
#new_htj => sub {my $t = new_htj();},
#output_htj => sub {output($global_htj)},
all_htj => sub {my $t = new_htj();output($t)},
) : (),
$use{'Template'} ? (
#new_tt => sub {my $t = new_tt();},
#output_tt => sub {output_tt($global_tt)},
process_tt => sub {output_tt($global_tt)},
$MEM_CACHE
? ()
: (all_tt_new_object => sub {my $t = new_tt();output_tt($t)}),
): (),
$use{'Template::Alloy'} ? (
#new_tt => sub {my $t = new_tt();},
#output_tt => sub {output_tt($global_tt)},
process_ta => sub {output_tt($global_ta)},
$MEM_CACHE
? ()
: (all_ta_new_object => sub {my $t = new_ta();output_tt($t)}),
): (),
$use{'Template::AutoFilter'} ? (
#new_ttaf => sub {my $t = new_ttaf();},
#output_ttaf => sub {output_ttaf($global_ttaf)},
process_ttaf => sub {output_ttaf($global_ttaf)},
$MEM_CACHE
? ()
: (all_ttaf_new_object => sub {my $t = new_ttaf();output_ttaf($t)}),
): (),
$use{'Template::HTML'} ? (
#new_tt => sub {my $t = new_tt();},
#output_tt => sub {output_tt($global_tt)},
process_tth => sub {output_tt($global_tth)},
$MEM_CACHE
? ()
: (all_tth_new_object => sub {my $t = new_tth();output_tt($t)}),
): (),
$use{'Text::Xslate'} ? (
#new_tt => sub {my $t = new_tt();},
#output_tt => sub {output_tt($global_tt)},
process_xslate => sub {output_xslate($global_xslate)},
$MEM_CACHE
? ()
: (all_xslate_new_object => sub {my $t = new_xslate();output_xslate($t)}),
): (),
$use{'Template::Like'} ? (
process_tl => sub {output_tl($global_tl)},
): (),
$use{'CGI::Ex::Template'} ? (
#new_tt => sub {my $t = new_tt();},
#output_tt => sub {output_tt($global_tt)},
process_cet => sub {output_tt($global_cet)},
$MEM_CACHE
? ()
: (all_cet_new_object => sub {my $t = new_cet();output_tt($t)}),
): (),
$use{'Text::ScriptTemplate'} ? (
#new_tst => sub {my $t = new_tst();},
#output_tst => sub {output_tst($global_tst)},
all_tst => sub {my $t = new_tst();output_tst($t)},
): (),
);
# try to align table correctly also for longer strings
my %args_new;
for my $key (keys %args) {
my $new_key = sprintf "%21s", $key;
$args_new{ $new_key } = $args{ $key };
}
if ($bench eq 'timethese') {
timethese ($iterations||-1, {
%args_new
});
}
elsif ($bench eq 'cmpthese') {
cmpthese ($iterations||-1, {
%args_new
});
}
}
__END__
HTML-Template-Compiled-1.001/examples/bench_mem.pl 000444 001750 001750 12411 12141164036 21075 0 ustar 00tina tina 000000 000000 #!/usr/bin/perl
# $Id: bench_mem.pl 1086 2009-07-03 16:45:27Z tinita $
use strict;
use warnings;
use lib "blib/lib";
$|=1;
use File::Copy;
use FindBin qw/ $RealBin /;
chdir "$RealBin/..";
# call perl examples/bench_mem.pl htc 1000
my ($mod, $count) = @ARGV;
usage() unless $mod;
mkdir "examples/mem";
mkdir "examples/memcache";
mkdir "examples/memcache/htc";
mkdir "examples/memcache/jit";
my %modules = (
tt => 'Template',
ht => 'HTML::Template',
htc => 'HTML::Template::Compiled',
htj => 'HTML::Template::JIT',
);
$count ||= 5;
eval "require $modules{$mod}";
#print HTML::Template::Compiled->VERSION,$/;
my %files = (
ht => 'test.htc',
htc => 'test.htc',
htj => 'test.htc',
tt => 'test.tt',
);
my @unique = keys %{ {reverse %files} };
sub new_htc {
my $t = HTML::Template::Compiled->new(
path => 'examples/mem',
loop_context_vars => 1,
filename => $_[0],
cache_dir => "examples/memcache/htc",
#cache => 0,
);
return $t;
}
sub new_ht {
my $t = HTML::Template->new(
path => 'examples/mem',
loop_context_vars => 1,
filename => $_[0],
cache => 1,
);
return $t;
}
sub new_htj {
my $t = HTML::Template::JIT->new(
path => ['examples/mem'],
loop_context_vars => 1,
filename => $_[0],
cache => 1,
jit_path => 'examples/memcache/jit',
);
return $t;
}
my $tt;
if ($mod eq 'tt') {
$tt = Template->new(
COMPILE_EXT => '.ttc',
COMPILE_DIR => 'examples/memcache/tt',
INCLUDE_PATH => 'examples/mem',
);
}
sub new_tt { return $tt }
my %params = (
name => '',
loopa => [{a=>3},{a=>4},{a=>5}],
#a => [qw(b c d)],
loopb => [{ inner => 23 }],
c => [
{
d=>[({F=>11},{F=>22}, {F=>33})]
},
{
d=>[({F=>44}, {F=>55}, {F=>66})]
}
],
if2 => 1,
if3 => 0,
blubber => "html ",
);
open OUT, ">>/dev/null";
#open OUT, ">&STDOUT";
my $cparam = $count;
my $ht_out = sub {
my $t = shift;
return unless defined $t;
$params{name} = (ref $t).' '.$cparam++;
$t->param(%params);
my $out = $t->output;
$t->param({});
print OUT $out;
};
my $outputs = {
ht => $ht_out,
htc => $ht_out,
htj => $ht_out,
tt => sub {
my ($t,$f) = @_;
return unless defined $t;
#print OUT "TT $f\n";
$t->process($f, \%params, \*OUT);
},
};
my $news = {
tt => \&new_tt,
ht => \&new_ht,
htc => \&new_htc,
htj => \&new_htj,
};
{
my $file = $files{$mod};
print "File $file\n";
-f "examples/mem/included.tt" or
copy "examples/included.tt", "examples/mem/included.tt" or die $!;
-f "examples/mem/included.htc" or
copy "examples/included.htc", "examples/mem/included.htc" or die $!;
# preprocess half of the templates
my $t = process($count/2, $mod, $file);
print_top("root");
for my $i (1..2) {
if (my $pid = fork) {
}
else {
print "Starting loop $i\n";
my $t = process($count, $mod, $file);
print_top($i);
print "End loop $i\n";
exit;
}
}
print "Waiting for child processes to finish\n";
wait;
print "Still waiting for child processes to finish\n";
wait;
print "Finished child processes\n";
print_top("root");
#;
}
sub print_top {
my $top = qx{top -b -n 1 |grep $$};
#my $top = qx{top -b -n 1 |grep perl};
chomp $top;
print "\ntop($_[0]) PID $$:\n$top\n";
}
sub process {
my ($count, $mod, $file) = @_;
for my $i ( 1 .. $count ) {
my $dup = sprintf "%s.%02d", $file, $i;
-f "examples/mem/$dup"
or copy "examples/$file", "examples/mem/$dup"
or die $!;
my $t;
if ($mod eq 'tt') {
$t = $tt;
}
else {
$t = $news->{$mod}->("$dup") or die $!;
}
#print STDERR "$mod '$t' loop '$i'\r";
$outputs->{$mod}->( $t, $dup ) or die $t->error;
#select undef, undef, undef, 1/($count/5);
}
}
sub usage {
print <<"EOM";
Usage: $0 (tt|ht|htc) num
Example: 100 iterations for TT:
$0 tt 100
EOM
exit;
}
__END__
-- with caching the template objects extra
:!perl examples/bench_mem.pl htj 500
File test.htc
htj 'tmpl_3588d6c4e3fc6254d1133a51e4c439b0' loop '500'
top: 744 tina 25 0 18980 16m 10m S 0.0 3.3 0:07.74 perl
:!perl examples/bench_mem.pl ht 500
File test.htc
ht 'HTML::Template=HASH(0x89ae50c)' loop '500'
top: 754 tina 25 0 11928 10m 2648 S 0.0 2.0 0:02.61 perl
:!perl examples/bench_mem.pl tt 500
File test.tt
tt 'Template=HASH(0xa184104)' loop '500'
top: 759 tina 24 0 36556 34m 2668 S 0.0 6.7 0:03.15 perl
:!perl examples/bench_mem.pl htc 500
File test.htc
htc 'HTML::Template::Compiled=ARRAY(0x94a0f44)' loop '500'
top: 764 tina 25 0 23272 21m 2732 S 0.0 4.2 0:01.54 perl
-- without caching the template objects extra
:!perl examples/bench_mem.pl htj 500
File test.htc
htj 'tmpl_3588d6c4e3fc6254d1133a51e4c439b0' loop '500'
top: 784 tina 25 0 18964 16m 10m S 0.0 3.3 0:07.76 perl
:!perl examples/bench_mem.pl ht 500
File test.htc
ht 'HTML::Template=HASH(0x88b62dc)' loop '500'
top: 792 tina 25 0 10900 9312 2648 S 0.0 1.8 0:02.66 perl
:!perl examples/bench_mem.pl tt 500
File test.tt
tt 'Template=HASH(0x8366640)' loop '500'
top: 797 tina 25 0 5744 4084 2668 S 0.0 0.8 0:03.62 perl
:!perl examples/bench_mem.pl htc 500
File test.htc
htc 'HTML::Template::Compiled=ARRAY(0x9498650)' loop '500'
top: 788 tina 25 0 23256 21m 2732 S 0.0 4.2 0:01.55 perl
HTML-Template-Compiled-1.001/examples/test.htc 000444 001750 001750 1562 12141164036 20267 0 ustar 00tina tina 000000 000000 ===test.html 1==========================================
name: look ma: ~
name with "":
INCLUDE: ((()))
---------------
loop a:
first? or last?
-----num:
item:
loop b:item: ROOT:
loop c
---------------
----num:
*firstlast inner item: odd
---------------------
if.if2! if.if3! no if.if3! no if.if3!!
===test.html ende==========================================
HTML-Template-Compiled-1.001/t 000755 001750 001750 0 12141164036 15074 5 ustar 00tina tina 000000 000000 HTML-Template-Compiled-1.001/t/32_compile_plugin.t 000444 001750 001750 1216 12141164036 20730 0 ustar 00tina tina 000000 000000 # $Id: 32_compile_plugin.t 966 2007-08-13 12:30:10Z tinita $
use warnings;
use strict;
use blib;
use lib 't';
use Test::More tests => 1;
use HTML::Template::Compiled;
use HTC_Utils qw($cache $tdir &cdir);
use HTC_Plugin;
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%homer beer=beercount %>
<%bart donut=donutcount %>
EOM
plugin => [qw(HTC_Plugin1 HTC_Plugin2)],
debug => 0,
);
$htc->param(
beercount => 3,
donutcount => 7,
);
my $out = $htc->output;
#print "out: $out\n";
cmp_ok($out, '=~', qr{Homer wants 3 beers.*Bart wants 7 donuts}s, "two plugins");
}
HTML-Template-Compiled-1.001/t/12_path.t 000444 001750 001750 4271 12141164036 16660 0 ustar 00tina tina 000000 000000 # Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl HTML-Template-Compiled.t'
# $Id: 12_path.t 985 2007-11-04 21:03:50Z tinita $
use Test::More tests => 5;
BEGIN { use_ok('HTML::Template::Compiled') };
use File::Spec ();
use lib 't';
use HTC_Utils qw($tdir);
eval {
my $htc = HTML::Template::Compiled->new(
path => [
File::Spec->catfile(qw(t templates_foo)),
$tdir,
],
filename => File::Spec->catfile(qw(a file1.html)),
search_path_on_include => 0,
#debug => 1,
);
};
print "err: $@\n" unless $ENV{HARNESS_ACTIVE};
my $f = File::Spec->catfile(qw/ a file1.html /);
cmp_ok($@, '=~', qr{'\Q$f\E' not found}, "search_path_on_include off");
my $htc = HTML::Template::Compiled->new(
path => ["$tdir/subdir", "$tdir/subdir2"],
filename => File::Spec->catfile(qw(a file1.html)),
search_path_on_include => 1,
#debug => 1,
);
my $out = $htc->output;
$out =~ tr/\r\n//d;
ok(
$out =~ m{Template t/templates/subdir/a/file1.htmlTemplate t/templates/subdir/a/file2.html},
"include form current dir"
);
{
# local $TODO = "path not yet correctly implemented";
my $out = '';
eval {
my $htc = HTML::Template::Compiled->new(
path => File::Spec->catfile(qw(t templates)),
filename => 'subdir/a/path.html',
search_path_on_include => 1,
);
$out = $htc->output;
#warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$out], ['out']);
};
#warn __PACKAGE__.':'.__LINE__.": error? $@\n";
cmp_ok($out, '=~', 'this is t/templates/subdir/b.html', 'search path option on include');
}
{
my $out = '';
eval {
my $htc = HTML::Template::Compiled->new(
path => File::Spec->catfile(qw(t templates)),
filename => 'subdir/a/path2.html',
search_path_on_include => 2,
);
$out = $htc->output;
# warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$out], ['out']);
};
# warn __PACKAGE__.':'.__LINE__.": error? $@\n";
cmp_ok($out, '=~', qr{this is t/templates/subdir/b.html.*this is templates/subdir/a/path2_inc.html}s, 'search current path and path option on include');
}
HTML-Template-Compiled-1.001/t/HTC_Plugin.pm 000444 001750 001750 4757 12141164036 17540 0 ustar 00tina tina 000000 000000 use strict;
use warnings;
use Data::Dumper;
{
package # hide from CPAN =)
HTC_Plugin1;
use HTML::Template::Compiled::Expression qw(:expressions);
HTML::Template::Compiled->register(__PACKAGE__);
sub register {
my ($class) = @_;
my %plugs = (
tagnames => {
HTML::Template::Compiled::Token::OPENING_TAG() => {
HOMER => [sub { exists $_[1]->{BEER} }, qw(BEER)],
},
},
compile => {
HOMER => {
open => sub {
my ($htc, $token, $args) = @_;
my $OUT = $args->{out};
my $attr = $token->get_attributes;
my $beer = $attr->{BEER};
my $varstr = $htc->get_compiler->parse_var($htc,
var => $beer,
method_call => $htc->method_call,
deref => $htc->deref,
formatter_path => $htc->formatter_path,
);
my $expression = _expr_literal(
<<"EOM"
$OUT "Homer wants " . $varstr . " beers";
EOM
);
return $expression->to_string;
},
},
},
);
return \%plugs;
}
}
#1;
#__END__
{
package # hide from CPAN =)
HTC_Plugin2;
use HTML::Template::Compiled::Expression qw(:expressions);
HTML::Template::Compiled->register(__PACKAGE__);
sub register {
my ($class) = @_;
my %plugs = (
tagnames => {
HTML::Template::Compiled::Token::OPENING_TAG() => {
BART => [sub { exists $_[1]->{DONUT} }, qw(DONUT)],
},
},
compile => {
BART => {
open => sub {
my ($htc, $token, $args) = @_;
my $OUT = $args->{out};
my $attr = $token->get_attributes;
my $beer = $attr->{DONUT};
my $varstr = $htc->get_compiler->parse_var($htc,
var => $beer,
method_call => $htc->method_call,
deref => $htc->deref,
formatter_path => $htc->formatter_path,
);
my $expression = _expr_literal(
<<"EOM"
$OUT "Bart wants " . $varstr . " donuts";
EOM
);
return $expression->to_string;
},
},
},
);
return \%plugs;
}
}
1;
HTML-Template-Compiled-1.001/t/08_global_vars.t 000444 001750 001750 3447 12141164036 20230 0 ustar 00tina tina 000000 000000 # Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl HTML-Template-Compiled.t'
# $Id: 08_global_vars.t 1092 2009-07-11 15:37:48Z tinita $
use Test::More tests => 4;
BEGIN { use_ok('HTML::Template::Compiled') };
{
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
filehandle => \*DATA,
global_vars => 1,
search_path_on_include => 1,
debug => 0,
case_sensitive => 0,
);
$htc->param(
global => 42,
outer => [
{
loopvar => 'one',
},
{
loopvar => 'two',
global => 23,
},
{
loopvar => 'three',
},
],
);
my $out = $htc->output;
#print $out, $/;
cmp_ok($out, '=~', qr{
loopvar:\ one.*global:\ 42.*\ included:.*
loopvar:\ two.*global:\ 23.*\ included:.*
loopvar:\ three.*global:\ 42.*\ included:.*
}xs, 'global_vars');
cmp_ok($out, "!~", "neverset", "global_vars and unset variable");
}
{
my $htc = HTML::Template::Compiled->new(
global_vars => 2,
scalarref => \<
EOM
debug => 0,
);
$htc->param(
a => {
b => { inner => 23 },
c => { inner => 42 },
},
);
my $out = $htc->output;
#print "($out)\n";
like($out, qr/^\s+23\s+42\s+$/, "global_vars => 2");
}
__DATA__
global:
loopvar:
global:
included:
neverset
HTML-Template-Compiled-1.001/t/06_dyn_include.t 000444 001750 001750 6167 12141164036 20232 0 ustar 00tina tina 000000 000000 use strict;
use warnings;
use Test::More tests => 19;
BEGIN { use_ok('HTML::Template::Compiled') };
use lib 't';
use HTC_Utils qw($tdir &cdir &create_cache &remove_cache);
my $cache_dir = "cache06";
$cache_dir = create_cache($cache_dir);
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
filename => 'dyn_include.htc',
# debug => 1,
# cache_debug => 1,
file_cache => 1,
file_cache_dir => $cache_dir,
);
#exit;
for my $ix (1..2,undef) {
for my $count (1..2) {
$htc->param(
file => (defined $ix? "dyn_included$ix.htc" : undef),
test => 23,
);
my $out;
eval {
$out = $htc->output;
};
if (defined $ix) {
#print $out;
$out =~ s/\r\n|\r/\n/g;
cmp_ok($out, "=~",
"Dynamic include:", "dynamic include $ix.1");
cmp_ok($out, "=~", "This is dynamically included file $ix\.", "dynamic include $ix.2");
cmp_ok($out, "=~", "23", "dynamic include $ix.3");
}
else {
#print "Error: $@\n";
#print "out: $out\n";
cmp_ok($out, "=~", 'Dynamic include:\s+$', "undefined filename");
}
}
}
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
inc: <%include_string foo %>
EOM
debug => 0,
);
$htc->param(
foo => 'included=<%= bar%>',
bar => 'real',
);
my $out = $htc->output;
#print "out: $out\n";
my $exp = 'inc: included=real';
cmp_ok($out, '=~', $exp, "include_string");
}
{
my $htc;
eval {
$htc = HTML::Template::Compiled->new(
filename => 'user_template.html',
path => 't/templates',
no_includes => 1,
);
};
my $error = "$@";
cmp_ok($error, '=~', 'Syntax error.*near.*include', "no_includes");
}
{
my $htc = HTML::Template::Compiled->new(
filename => "wrapped.html",
path => 't/templates',
# debug => 1,
loop_context_vars => 1,
cache => 0,
);
$htc->param(
foo => 23,
);
my $out = $htc->output;
my $exp = <<"EOM";
wrapper:
wrapped in wrapper.html: foo: 23
wrapped in wrapper2.html: foo2: 23
wrapped in wrapper1.html: foo1: 23
EOM
#warn __PACKAGE__.':'.__LINE__.": $out\n";
for ($out, $exp) {
s/[\r\n]/ /g;
tr/ / /s;
}
cmp_ok($out, 'eq', $exp, "wrapper");
$out = File::Spec->catfile('t', 'templates', 'out_fh.htc.output');
open my $fh, '>', $out or die $!;
$htc = HTML::Template::Compiled->new(
filename => "wrapped.html",
path => 't/templates',
# debug => 1,
loop_context_vars => 1,
cache => 0,
out_fh => 1,
);
$htc->param(
foo => 23,
);
$htc->output($fh);
close $fh;
open $fh, "<", $out or die $!;
my $out2 = do { local $/; <$fh> };
#warn __PACKAGE__.':'.__LINE__.": $out2\n";
for ($out2) {
s/[\r\n]/ /g;
tr/ / /s;
}
cmp_ok($out2, 'eq', $exp, "wrapper out_fh");
unlink $out;
}
HTML::Template::Compiled->clear_filecache($cache_dir);
remove_cache($cache_dir);
__END__
Dynamic include:
This is dynamically included file 1.
23
HTML-Template-Compiled-1.001/t/36_default.t 000444 001750 001750 1403 12141164036 17350 0 ustar 00tina tina 000000 000000 # Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl HTML-Template-Compiled.t'
# $Id: 13_loop.t 1042 2008-05-10 21:51:45Z tinita $
use lib 'blib/lib';
use Test::More tests => 2;
BEGIN { use_ok('HTML::Template::Compiled') };
use lib 't';
use HTC_Utils qw($cache $tdir &cdir);
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
EOM
debug => 0,
loop_context_vars => 1,
path => $tdir,
);
$htc->param(
list => [qw(a b c d e f g h)]
);
my $out = $htc->output;
$out =~ s/\s+/ /g;
#print $out, $/;
cmp_ok($out, 'eq','foo ', "loop context vars in include");
}
HTML-Template-Compiled-1.001/t/HTC_Utils.pm 000444 001750 001750 1207 12141164036 17365 0 ustar 00tina tina 000000 000000 package # hide from cpan =)
HTC_Utils;
use base 'Exporter';
use File::Spec;
@EXPORT_OK = qw($cache $cache_lock $tdir &cdir &create_cache &remove_cache);
$cache = File::Spec->catdir(qw(t cache));
$cache_lock = File::Spec->catdir(qw(t cache lock));
$tdir = File::Spec->catdir(qw(t templates));
sub cdir { File::Spec->catdir(@_) }
sub create_cache {
my ($dir) = @_;
my $cache = File::Spec->catdir('t', $dir);
mkdir $cache;
return $cache;
}
sub remove_cache {
my ($dir) = @_;
$dir ||= 'cache';
my $cache = $dir;
my $cache_lock = File::Spec->catdir($dir, 'lock');
unlink $cache_lock;
rmdir $cache;
}
1;
HTML-Template-Compiled-1.001/t/04_out_fh.t 000444 001750 001750 2474 12141164036 17214 0 ustar 00tina tina 000000 000000 use Test::More tests => 5;
BEGIN { use_ok('HTML::Template::Compiled') };
use lib 't';
use File::Spec;
use HTC_Utils qw($tdir &cdir &create_cache &remove_cache);
my $cache_dir = "cache04";
$cache_dir = create_cache($cache_dir);
my $out = File::Spec->catfile('t', 'templates', 'out_fh.htc.output');
HTML::Template::Compiled->clear_filecache($cache_dir);
test('compile', 'clearcache');
test('filecache');
test('memcache', 'clearcache');
HTML::Template::Compiled->preload($cache_dir);
test('after preload', 'clearcache');
sub test {
my ($type, $clearcache) = @_;
# test output($fh)
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
filename => 'out_fh.htc',
out_fh => 1,
file_cache_dir => $cache_dir,
file_cache => 1,
);
open my $fh, '>', $out or die $!;
$htc->output($fh);
close $fh;
open my $f, '<', File::Spec->catfile('t', 'templates', 'out_fh.htc') or die $!;
open my $t, '<', File::Spec->catfile('t', 'templates', 'out_fh.htc.output') or die $!;
local $/;
my $orig = <$f>;
my $test = <$t>;
for ($orig, $test) {
tr/\n\r//d;
}
ok($orig eq $test, "out_fh $type");
$htc->clear_cache() if $clearcache;
# this is not portable
#ok(-s $out == -s File::Spec->catfile('t', 'out_fh.htc'), "out_fh");
}
unlink $out;
HTML::Template::Compiled->clear_filecache($cache_dir);
remove_cache($cache_dir);
HTML-Template-Compiled-1.001/t/07_formatter.t 000444 001750 001750 2113 12141164036 17724 0 ustar 00tina tina 000000 000000 # Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl HTML-Template-Compiled.t'
# $Id: 07_formatter.t 751 2006-10-11 21:52:50Z tinita $
use lib 'blib/lib';
use Test::More tests => 2;
BEGIN { use_ok('HTML::Template::Compiled::Formatter') };
my $formatter = {
'HTC::Class1' => {
fullname => sub {
$_[0]->first . ' ' . $_[0]->last
},
first => HTC::Class1->can('first'),
last => HTC::Class1->can('last'),
},
};
my $htc = HTML::Template::Compiled::Formatter->new(
path => 't/templates',
filename => 'formatter.htc',
debug => 0,
);
my $obj = bless ({ first => 'Abi', last => 'Gail'}, 'HTC::Class1');
$htc->param(
test => 23,
obj => $obj,
);
local $HTML::Template::Compiled::Formatter::formatter = $formatter;
my $out = $htc->output;
my $exp = <{first}
}
sub HTC::Class1::last {
$_[0]->{last}
}
__END__
<%= test%>
<%= obj/first %> plus <%= obj/last%>
<%= obj/fullname%>
HTML-Template-Compiled-1.001/t/33_plugins.t 000444 001750 001750 5566 12141164036 17420 0 ustar 00tina tina 000000 000000
# $Id: 33_plugins.t 1104 2009-08-31 08:46:36Z tinita $
use warnings;
use strict;
use blib;
use lib 't';
use Test::More tests => 5;
use HTML::Template::Compiled;
use HTC_Utils qw($cache $tdir &cdir);
for (0..1) {
my $plug = bless(
{}, 'HTC_Test'
);
HTML::Template::Compiled->register($plug);
sub HTC_Test::register {
my ($class) = @_;
my %plugs = (
escape => {
TESTING => sub {
my ($arg) = @_;
return "$_$arg$arg";
},
},
);
return \%plugs;
}
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%= foo escape=testing %>
EOM
plugin => [$plug],
debug => 0,
cache => 0,
);
my $string = 'string';
$htc->param(
foo => $string,
);
my $out = $htc->output;
#print "out: $out\n";
cmp_ok($out, '=~', "$_$string$string", "plugin as object $_");
}
{
my $plug = bless(
{
'lang' => 'en',
'map' => {
en => {
HELLO_WORLD => 'Hello world',
},
de => {
HELLO_WORLD => 'Hallo Welt',
},
es => {
HELLO_WORLD => 'Hola Mundo',
},
},
}, 'HTC_Test2'
);
HTML::Template::Compiled->register($plug);
sub HTC_Test2::translate {
my ($self, $id) = @_;
return $self->{map}->{ $self->{lang} }->{$id};
}
sub HTC_Test2::register {
my ($class) = @_;
my %plugs = (
tagnames => {
HTML::Template::Compiled::Token::OPENING_TAG() => {
TRANSLATE => [sub { exists $_[1]->{ID} }, 'ID'],
},
},
compile => {
TRANSLATE => {
open => sub {
my ($htc, $token, $args) = @_;
my $OUT = $args->{out};
my $attr = $token->get_attributes;
my $expression = <<"EOM";
$OUT "Translation of $attr->{ID}: ";
$OUT \$t->get_plugin('HTC_Test2')->translate('\Q$attr->{ID}\E');
EOM
return $expression;
},
},
},
);
return \%plugs;
}
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%translate id="HELLO_WORLD" %>
EOM
plugin => [$plug],
debug => 0,
cache => 0,
);
my $string = 'string';
for my $lang (qw/ en de es /) {
$plug->{lang} = $lang;
my $translated = $plug->{map}->{$lang}->{HELLO_WORLD};
my $out = $htc->output;
#print "out: $out\n";
cmp_ok($out, '=~', "$translated", "plugin as object $lang");
}
}
HTML::Template::Compiled->clear_filecache($cache);
HTML-Template-Compiled-1.001/t/35_debug.t 000444 001750 001750 6700 12141164036 17016 0 ustar 00tina tina 000000 000000 use warnings;
use strict;
use lib 't';
use Test::More tests => 9;
use_ok('HTML::Template::Compiled');
use HTC_Utils qw($tdir &cdir &create_cache &remove_cache);
my $cache_dir = "cache35";
$cache_dir = create_cache($cache_dir);
sub HTML::Template::Compiled::Test::bar {
return $_[0]->[0]
}
sub HTML::Template::Compiled::Test::baz {
return $_[0]->[1]
}
local $HTML::Template::Compiled::DEBUG = 0;
{
local $HTML::Template::Compiled::DEBUG = 1;
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%= /foo.bar %>
<%= /foo.boo %>
<%= /foo.baz %>
EOM
debug => 0,
);
my $obj = bless [23, 24], 'HTML::Template::Compiled::Test';
$htc->param(foo => $obj);
my $out;
eval {
$out = $htc->output;
};
ok($@, "Exception");
if ($@) {
#warn __PACKAGE__.':'.__LINE__.": $@\n";
my $msg = $htc->debug_code;
my $msg_html = $htc->debug_code(1);;
#print $msg, $/;
#print $msg_html, $/;
cmp_ok($msg, '=~', qr/ ERROR line (\d+)/, 'Error message');
}
else {
ok(0, 'Exception');
}
}
{
my %exp = (
mc => {
0 => {
count => {
1 => '### HTML::Template::Compiled Cache Debug ### FILE CACHE MISS: simple.tmpl',
2 => '### HTML::Template::Compiled Cache Debug ### FILE CACHE HIT: simple.tmpl',
},
},
1 => {
count => {
1 => '### HTML::Template::Compiled Cache Debug ### MEM CACHE MISS: simple.tmpl',
2 => '### HTML::Template::Compiled Cache Debug ### MEM CACHE HIT: simple.tmpl',
},
},
2 => {
count => {
1 => '### HTML::Template::Compiled Cache Debug ### MEM CACHE MISS: simple.tmpl'
. '### HTML::Template::Compiled Cache Debug ### FILE CACHE MISS: simple.tmpl',
2 => '### HTML::Template::Compiled Cache Debug ### MEM CACHE HIT: simple.tmpl',
},
},
},
);
for my $mc (0, 1, 2) {
my $memcache = 0;
my $file_cache = 1;
my $file_cache_dir = $cache_dir;
if ($mc == 1) {
$memcache = 1;
$file_cache = 0;
$file_cache_dir = '';
}
elsif ($mc == 2) {
$memcache = 1;
}
my %args = (
filename => "simple.tmpl",
path => $tdir,
cache => $memcache,
file_cache => $file_cache,
file_cache_dir => $file_cache_dir,
cache_debug => [qw/ mem_hit mem_miss file_hit file_miss /],
);
for my $count (1..2) {
my $warn = '';
{
local $SIG{__WARN__} = sub {
$warn .= shift;
};
my $htc = HTML::Template::Compiled->new(
%args,
);
if ($count == 2) {
$htc->clear_cache();
HTML::Template::Compiled->clear_filecache($cache_dir);
}
}
$warn =~ s/[\r\n]//g;
my $exp = $exp{mc}->{$mc}->{count}->{$count};
my $cache_string = $mc == 0 ? "file cache" : $mc == 1 ? "mem cache" : "file and mem cache";
cmp_ok($warn, 'eq', $exp, "cache=$cache_string count=$count");
}
}
}
HTML::Template::Compiled->clear_filecache($cache_dir);
remove_cache($cache_dir);
HTML-Template-Compiled-1.001/t/03_param.t 000444 001750 001750 4357 12141164036 17031 0 ustar 00tina tina 000000 000000 # $Id: 03_param.t 1128 2011-10-31 19:59:56Z tinita $
use Test::More tests => 12;
BEGIN {
use_ok('HTML::Template::Compiled');
use_ok('HTML::Template::Compiled::Classic');
}
{
local $HTML::Template::Compiled::DEFAULT_QUERY = 1;
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
scalarref => \' ',
);
$htc->param(
this => {
is => [qw(a test for param)],
returning => 'the value for a parameter',
},
);
my $test = $htc->param("this");
ok($test->{is}->[3] eq 'param', "param('var')");
my @param = sort $htc->param();
#print "(@param)\n";
cmp_ok(@param, "==", 2, "param() 1");
cmp_ok($param[0], "eq", 'bar', "param() 2");
cmp_ok($param[1]||'', "eq", 'foo', "param() 2");
eval {
my @query = sort $htc->query();
cmp_ok("@param", "eq", "@query", "query");
};
param_accumulates: {
$htc->clear_params;
$htc->param({ foo => 'foo value' });
like($htc->output, qr/foo value/);
$htc->param({ bar => 'bar value' });
like($htc->output, qr/foo value/);
}
literal_dot_is_ok: {
# To be compatible with H::T, we need
# to first check if a dot is literal
# part of the name before treating it magically.
# This is important for a smooth upgrade path.
my $htc = HTML::Template::Compiled::Classic->new(
path => 't/templates',
scalarref => \'',
);
$htc->param('foo.bar', 'WORKS');
like($htc->output, qr/WORKS/, "HTC::Classic literal dot is OK");
}
subref_variables: {
my $htc = HTML::Template::Compiled::Classic->new(
scalarref => \"",
global_vars => 1,
);
$htc->param(
foo => sub { return "bar" },
loop => [{a=>23}],
);
my $out = $htc->output;
#print "($out)\n";
cmp_ok($out, 'eq', "bar23bar", "HTC::Classic subref variables");
}
case_sensitive: {
my $htc = HTML::Template::Compiled::Classic->new(
scalarref => \" & ",
case_sensitive => 0,
);
$htc->param(
foo => 'Harold',
bar => 'Maude',
);
my $out = $htc->output;
#print "($out)\n";
cmp_ok($out, 'eq', "Harold & Maude", "HTC::Classic case_sensitive");
}
}
HTML-Template-Compiled-1.001/t/38_vars.t 000444 001750 001750 1730 12141164036 16704 0 ustar 00tina tina 000000 000000 # $Id: 13_loop.t 1077 2008-09-01 19:02:06Z tinita $
use Test::More tests => 3;
BEGIN { use_ok('HTML::Template::Compiled') };
use lib 't';
use
HTC_Utils qw($cache $tdir &cdir);
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%set_var FOO value=.root.foo %>
<%= FOO %>
<%include var_include.html %>
EOM
debug => 0,
loop_context_vars => 1,
path => $tdir,
);
$htc->param(
root => {
foo => 23,
},
);
my $out = $htc->output;
$out =~ s/\s+//g;
cmp_ok($out, "eq", "2323", "set_var, use_vars");
#print "out: $out\n";
}
{
eval {
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%set_var name="foo bar" value=.root %>
EOM
debug => 0,
loop_context_vars => 1,
path => $tdir,
);
};
my $error = $@;
# warn __PACKAGE__.':'.__LINE__.": $error\n";
cmp_ok($error, "=~", ".*Syntax error in .*", "invalid set_var");
}
HTML-Template-Compiled-1.001/t/10_if_else.t 000444 001750 001750 2654 12141164036 17333 0 ustar 00tina tina 000000 000000 # $Id: 10_if_else.t 1079 2008-11-03 18:57:01Z tinita $
use lib 'blib/lib';
use Test::More tests => 3;
BEGIN { use_ok('HTML::Template::Compiled') };
use File::Spec;
my $cache = File::Spec->catfile('t', 'cache');
HTML::Template::Compiled->clear_filecache($cache);
test_defined();
test_double_else();
sub test_defined {
my ($type, $clearcache) = @_;
my $str = <<'EOM';
WRONGWRONGRIGHT
RIGHTWRONGRIGHT
RIGHTRIGHTWRONG
RIGHT
EOM
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
scalarref => \$str,
#debug => 1,
);
$htc->param(
'undef' => undef,
'zero' => 0,
'true' => 'a true value',
);
my $out = $htc->output;
#print $out;
my @right = $out =~ m/RIGHT/g;
my @wrong = $out =~ m/WRONG/g;
ok(@right == 4 && @wrong == 0, "if defined");
}
sub test_double_else {
my $text = qq{Before.
1.
2.
3. After.};
eval {
my $template = HTML::Template::Compiled->new(
debug => 1,
scalarref => \$text,
);
};
#print $@, $/;
like($@, qr/\Q'TMPL_ELSE' does not match opening tag (ELSE)/,
"including 2 tags for one tmpl_if should throw an error");
}
HTML-Template-Compiled-1.001/t/29_encoding.t 000444 001750 001750 2422 12141164036 17516 0 ustar 00tina tina 000000 000000 # $Id: 29_encoding.t 1151 2012-04-21 21:46:30Z tinita $
use warnings;
use strict;
use blib;
use lib 't';
use Test::More tests => 2;
use_ok('HTML::Template::Compiled');
use HTC_Utils qw($cache $tdir &cdir);
eval { require URI::Escape };
my $uri = $@ ? 0 : 1;
eval { require Encode };
my $encode = $@ ? 0 : 1;
eval { require HTML::Entities };
my $ent = $@ ? 0 : 1;
my $template = File::Spec->catfile(qw/ t templates utf8.htc /);
#use Devel::Peek;
SKIP: {
skip "no URI::Escape, Encode, HTML::Entities installed", 1 unless $uri && $encode && $ent;
open my $fh, '>:utf8', $template;
my $string = <<"EOM";
test utf8: \x{f6}
<%= utf8 escape=url %>
<%= utf8 escape=html_all %>
EOM
print $fh $string;
#Dump $string;
close $fh;
my $htc = HTML::Template::Compiled->new(
filename => 'utf8.htc',
path => $tdir,
debug => 0,
open_mode => '<:utf8',
);
my $u = "ä";
$u = Encode::decode_utf8($u);
$htc->param(
utf8 => $u,
);
my $out = $htc->output;
my $test = "\x{f6}";
#Dump $test;
$test = Encode::encode('utf-8', $test);
#Dump $test;
Encode::_utf8_on($test);
#print "out: $out\n";
#Dump $out;
cmp_ok($out, '=~', qr{$test.*%C3%A4.*ä}is, "uri_escape_utf8");
unlink $template;
}
HTML-Template-Compiled-1.001/t/13_loop.t 000444 001750 001750 10223 12141164036 16710 0 ustar 00tina tina 000000 000000 use Test::More tests => 10;
BEGIN { use_ok('HTML::Template::Compiled') };
use lib 't';
use HTC_Utils qw($tdir &cdir &create_cache &remove_cache);
my $cache_dir = "cache13";
$cache_dir = create_cache($cache_dir);
for my $new_alias (0,1) {
local $HTML::Template::Compiled::Compiler::DISABLE_NEW_ALIAS = 1 unless ($new_alias);
my $htc = HTML::Template::Compiled->new(
scalarref => \<<"EOM",
EOM
debug => 0,
loop_context_vars => 1,
cache => 0,
);
my $array = [];
my $array2 = [];
if ($new_alias) {
$array = [qw(a b c)];
$array2 = [{ foo => 'a' }, { foo => 'b' }, { foo => 'c' }];
}
else {
$array = [{ '$iterator' => 'a' }, { '$iterator' => 'b' }, { '$iterator' => 'c' }];
$array2 = [{ '$iterator' => { foo => 'a' } }, { '$iterator' => { foo => 'b' } }, { '$iterator' => { foo => 'c' } }];
}
$htc->param(
array => $array,
array2 => $array2,
);
my $out = $htc->output;
$out =~ s/\s+//g;
if ($new_alias) {
cmp_ok($out, "eq", "aabbccaabbcc", "tmpl_loop array alias=iterator");
}
else {
cmp_ok($out, "=~", qr{HASH\(.*\)aHASH\(.*\)bHASH\(.*\)cabc}, "tmpl_loop array alias=iterator");
}
#print "out: $out\n";
}
my $text1 = <<'EOM';
|
(outer:)
(even:)
EOM
for (0,1) {
my $htc = HTML::Template::Compiled->new(
scalarref => \$text1,
debug => 0,
loop_context_vars => $_,
);
$htc->param(array => [
{x=>"a","__counter__"=>"A","__outer__"=>"OUTER"},
{x=>"b","__counter__"=>"B","__even__"=>"EVEN"},
{x=>"c","__counter__"=>"C",},
{x=>"d","__counter__"=>"D",,"__even__"=>"EVEN"},
{x=>"e","__counter__"=>"E","__outer__"=>"OUTER"},
]);
my $out = $htc->output;
$out =~ s/\s+//g;
my $exp;
if ($_ == 1) {
$exp = "|(outer:1)1a|(even:1)2b|3c|(even:1)4d|(outer:1)5e";
}
else {
$exp = "|(outer:OUTER)Aa|(even:EVEN)Bb|Cc|(even:EVEN)Dd|(outer:OUTER)Ee";
}
#print "($out)($exp)\n";
cmp_ok($out, "eq", $exp, "loop context");
}
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<%= _ %><%/loop list %>
EOM
debug => 0,
);
$htc->param(
list => [qw(a b c)]
);
my $out = $htc->output;
$out =~ s/^\s+//;
$out =~ s/\s+\z//;
#print $out, $/;
cmp_ok($out, 'eq','a, b, c', "loop join attribute");
}
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<%= _ %><%if __break__%>.<%/if %><%/loop list %>
EOM
debug => 0,
loop_context_vars => 1,
);
$htc->param(
list => [qw(a b c d e f g h)]
);
my $out = $htc->output;
$out =~ s/^\s+//;
$out =~ s/\s+\z//;
#print $out, $/;
cmp_ok($out, 'eq','abc.def.gh', "loop break attribute");
}
{
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%loop list %>
<%include loop_included.tmpl %>
<%/loop list %>
EOM
debug => 0,
loop_context_vars => 1,
path => $tdir,
);
$htc->param(
list => [qw(a b c d e f g h)]
);
my $out = $htc->output;
$out =~ s/\s+/ /g;
#print $out, $/;
cmp_ok($out, 'eq',' 0 1 2 3 4 5 6 7 h ', "loop context vars in include");
}
for (0, 1) {
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
<%loop foo.list %>
<%= a %>
<%/loop foo.list %>
EOM
debug => 0,
loop_context_vars => 1,
path => $tdir,
cache => 0,
file_cache => 1,
file_cache_dir => $cache_dir,
);
$htc->param(
foo => {
list => [{a => 1},{a => 2},{a => 3}],
},
);
my $out = $htc->output;
$out =~ s/\s+/ /g;
#print $out, $/;
cmp_ok($out, 'eq',' 1 2 3 ', "loop " . ($_ ? "after" : "before") . " caching");
}
HTML::Template::Compiled->clear_filecache($cache_dir);
remove_cache($cache_dir);
HTML-Template-Compiled-1.001/t/19_query.t 000444 001750 001750 6210 12141164036 17073 0 ustar 00tina tina 000000 000000 use warnings;
use strict;
use Test::More tests => 5;
use lib 't';
use HTC_Utils qw($tdir &cdir &create_cache &remove_cache);
my $cache_dir = "cache19";
$cache_dir = create_cache($cache_dir);
# test query() (From HTML::Template test suite)
use HTML::Template::Compiled;
use HTML::Template::Compiled::Lazy;
use File::Copy;
use Fcntl qw(:seek);
my $file_orig = File::Spec->catfile(qw(t templates query-test.tmpl));
my $file_copy = File::Spec->catfile(qw(t templates query-test-copy.tmpl));
copy($file_orig, $file_copy);
chmod 0644, $file_copy;
my $ok1 = query_template();
ok($ok1, "query 1");
#print `ls t/cache`;
if (1) {
my $htc = HTML::Template::Compiled::Lazy->new(
scalarref => \"<%= foo%>",
use_query => 1,
expire_time => 1,
);
my @params;
eval {
@params = $htc->query;
};
cmp_ok("@params", 'eq', 'foo', 'HTC::Lazy and query()');
}
sleep 3;
{
open my $fh, '+<', $file_copy or die $!;
local $/;
my $data = <$fh>;
seek $fh, SEEK_SET, 0;
truncate $fh, 0;
$data =~ s/EXAMPLE_INNER_LOOP/EXAMPLE_INNER_LOOP_TEST/;
print $fh $data;
close $fh;
}
my $ok2 = query_template();
ok(!$ok2, "query 2");
#exit;
sub query_template {
my $template = HTML::Template::Compiled->new(
path => 't/templates',
filename => 'query-test-copy.tmpl',
file_cache_dir => $cache_dir,
file_cache => 1,
expire_time => 1,
use_query => 1,
);
my %params;
eval {
%params = map {$_ => 1} $template->query(loop => 'EXAMPLE_LOOP');
};
my @result;
eval {
@result = $template->query(loop => ['EXAMPLE_LOOP', 'BEE']);
};
my $ok = (
$@ =~ /error/ and
$template->query(name => 'var') eq 'VAR' and
$template->query(name => 'included_var') eq 'VAR' and
#$template->query(name => 'included_var2') eq 'VAR' and
$template->query(name => 'EXAMPLE_LOOP') eq 'LOOP' and
exists $params{bee} and
exists $params{bop} and
exists $params{example_inner_loop} and
$template->query(name => ['EXAMPLE_LOOP', 'EXAMPLE_INNER_LOOP']) eq 'LOOP'
);
my $out = $template->output;
$template->clear_cache;
return $ok;
print "out: $out\n";
}
{
# test query() (From HTML::Template test suite)
my $template = HTML::Template::Compiled->new(
path => 't/templates',
filename => 'query-test2.tmpl',
use_query => 1,
);
my %p;
eval { %p = map {$_ => 1} $template->query(loop => ['LOOP_FOO', 'LOOP_BAR']); };
ok(exists $p{foo} and exists $p{bar} and exists $p{bash}, "foo bar");
$template->clear_cache;
}
{
my $template = HTML::Template::Compiled->new(
path => 't/templates',
filename => 'query-test2.tmpl',
use_query => 0,
);
my $warn = '';
{
local $SIG{__WARN__} = sub { $warn .= shift };
my $test = $template->query(loop => ['LOOP_FOO', 'LOOP_BAR']);
}
cmp_ok($warn, '=~', qr{\QYou are using query() but have not specified that you want to use it}, "no use_query but using query()");
}
unlink $file_copy;
HTML::Template::Compiled->clear_filecache($cache_dir);
remove_cache($cache_dir);
HTML-Template-Compiled-1.001/t/09_wrong.t 000444 001750 001750 5175 12141164036 17072 0 ustar 00tina tina 000000 000000 # Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl HTML-Template-Compiled.t'
# $Id: 09_wrong.t 511 2006-08-07 01:41:42Z tinita $
use Test::More tests => 11;
BEGIN { use_ok('HTML::Template::Compiled') };
eval {
my $htc = HTML::Template::Compiled->new(
scalarref => \<<'EOM',
bar
<%if foo %>bar<%/if%>
<%if foo %>bar
EOM
debug => $ENV{HARNESS_ACTIVE} ? 0 : 1,
line_numbers => 1,
);
};
print "err: $@\n" unless $ENV{HARNESS_ACTIVE};
ok($@ =~ m/Missing closing tag for 'IF'/, "premature end of template");
# test wrong balanced tag
my $wrong;
eval {
$wrong = HTML::Template::Compiled->new(
path => 't/templates',
line_numbers => 1,
filename => 'wrong.html',
debug => $ENV{HARNESS_ACTIVE} ? 0 : 1,
);
};
print "err: $@\n" unless $ENV{HARNESS_ACTIVE};
ok($@ =~ m/does not match opening tag/ , "wrong template");
eval {
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
filename => 'notexist.htc',
debug => $ENV{HARNESS_ACTIVE} ? 0 : 1,
);
};
print "err: $@\n" unless $ENV{HARNESS_ACTIVE};
ok($@ =~ m/not found/ , "template not found");
eval {
my $str = <<'EOM';
EOM
my $htc = HTML::Template::Compiled->new(
path => 't/templates',
scalarref => \$str,
debug => $ENV{HARNESS_ACTIVE} ? 0 : 1,
);
};
print "err: $@\n" unless $ENV{HARNESS_ACTIVE};
ok($@ =~ m/not found/ , "template from include not found");
{
my @wrong = (
"",
"",
"",
"foo",
);
for my $wrong (@wrong) {
eval {
my $htc = HTML::Template::Compiled->new(
scalarref => \$wrong,
debug => 0,
);
};
print STDERR "Error? $@\n" unless $ENV{HARNESS_ACTIVE};;
cmp_ok($@, "=~", qr{\Q: Syntax error in tag at }, "die when syntax is wrong");
}
}
{
my $tmpl = <<"EOM";
end
EOM
for my $strict (0, 1) {
my $out = '';
eval {
my $htc = HTML::Template::Compiled->new(
scalarref => \$tmpl,
strict => $strict,
);
$htc->param(foo => 23);
$out = $htc->output;
};
my $err = $@;
if ($strict) {
cmp_ok($err, '=~', qr{\Q Syntax error in tag at }, "unknown tag strict");
}
else {
$out =~ s/\s+/ /g;
my $exp = '23