| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Typist::Template::Tags; |
|
2
|
1
|
|
|
1
|
|
1598
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
45
|
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
37
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
530
|
use Typist::Template::Context qw( pass_tokens_else ); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
63
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
7
|
use vars qw( $VERSION ); |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
1194
|
|
|
8
|
|
|
|
|
|
|
$VERSION = 0.34; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $PREFIX = Typist->instance->prefix; |
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub import { |
|
13
|
1
|
|
|
1
|
|
15
|
Typist::Template::Context->add_tag(SetVar => \&var_handler); |
|
14
|
1
|
|
|
|
|
5
|
Typist::Template::Context->add_tag(GetVar => \&var_handler); |
|
15
|
1
|
|
|
|
|
4
|
Typist::Template::Context->add_container_tag(SetVarBlock => \&var_handler); |
|
16
|
1
|
|
|
|
|
4
|
Typist::Template::Context->add_conditional_tag(If => \&conditional_handler); |
|
17
|
1
|
|
|
|
|
4
|
Typist::Template::Context->add_conditional_tag( |
|
18
|
|
|
|
|
|
|
Unless => \&conditional_handler); |
|
19
|
1
|
|
|
|
|
3
|
Typist::Template::Context->add_conditional_tag( |
|
20
|
|
|
|
|
|
|
IfOne => \&conditional_handler); |
|
21
|
1
|
|
|
|
|
4
|
Typist::Template::Context->add_conditional_tag( |
|
22
|
|
|
|
|
|
|
UnlessEmpty => \&conditional_handler); |
|
23
|
1
|
|
|
|
|
4
|
Typist::Template::Context->add_conditional_tag( |
|
24
|
|
|
|
|
|
|
UnlessZero => \&conditional_handler); |
|
25
|
1
|
|
|
|
|
4
|
Typist::Template::Context->add_container_tag(Else => \&pass_tokens_else) |
|
26
|
|
|
|
|
|
|
; # right thing? |
|
27
|
1
|
|
|
|
|
10
|
Typist::Template::Context->add_container_tag(Include => \&include); |
|
28
|
1
|
|
|
0
|
|
5
|
Typist::Template::Context->add_container_tag(Ignore => sub { '' }); |
|
|
0
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub var_handler { |
|
32
|
0
|
|
|
0
|
0
|
|
my ($ctx, $args, $cond) = @_; |
|
33
|
0
|
|
|
|
|
|
my $tag = $ctx->stash('tag'); |
|
34
|
|
|
|
|
|
|
return |
|
35
|
0
|
0
|
0
|
|
|
|
$ctx->error( |
|
36
|
|
|
|
|
|
|
Typist->translate( |
|
37
|
|
|
|
|
|
|
"You used a [_1] tag without any arguments.", |
|
38
|
|
|
|
|
|
|
"<$PREFIX$tag>" |
|
39
|
|
|
|
|
|
|
) |
|
40
|
|
|
|
|
|
|
) |
|
41
|
|
|
|
|
|
|
unless keys %$args && $args->{name}; |
|
42
|
0
|
0
|
|
|
|
|
if ($tag eq 'SetVar') { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
my $val = defined $args->{value} ? $args->{value} : ''; |
|
44
|
0
|
|
|
|
|
|
$ctx->var($args->{name}, $val); |
|
45
|
0
|
|
|
|
|
|
return ''; |
|
46
|
|
|
|
|
|
|
} elsif ($tag eq 'GetVar') { |
|
47
|
0
|
0
|
|
|
|
|
my $val = |
|
48
|
|
|
|
|
|
|
defined $ctx->var($args->{name}) |
|
49
|
|
|
|
|
|
|
? $ctx->var($args->{name}) |
|
50
|
|
|
|
|
|
|
: $args->{default}; |
|
51
|
0
|
0
|
|
|
|
|
return $ctx->error("Uninitialized value in <$tag>.") |
|
52
|
|
|
|
|
|
|
unless defined $val; |
|
53
|
0
|
|
|
|
|
|
return $val; |
|
54
|
|
|
|
|
|
|
} elsif ($tag eq 'SetVarBlock') { |
|
55
|
0
|
|
|
|
|
|
my $builder = $ctx->stash('builder'); |
|
56
|
0
|
|
|
|
|
|
my $tokens = $ctx->stash('tokens'); |
|
57
|
0
|
0
|
|
|
|
|
defined(my $out = $builder->build($ctx, $tokens, $cond)) |
|
58
|
|
|
|
|
|
|
or return $ctx->error($builder->errstr); |
|
59
|
0
|
|
|
|
|
|
$ctx->var($args->{name}, $out); |
|
60
|
0
|
|
|
|
|
|
return ''; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
} |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub conditional_handler { |
|
65
|
0
|
|
|
0
|
0
|
|
my ($ctx, $args, $cond) = @_; |
|
66
|
0
|
|
|
|
|
|
my $tag = $ctx->stash('tag'); |
|
67
|
0
|
0
|
0
|
|
|
|
$ctx->error($PREFIX . "$tag requires a 'name' or 'tag' argument.") |
|
68
|
|
|
|
|
|
|
if defined $args->{name} || defined $args->{tag}; |
|
69
|
0
|
|
|
|
|
|
my $val; |
|
70
|
0
|
0
|
|
|
|
|
if (defined $args->{name}) { |
|
71
|
0
|
|
0
|
|
|
|
$val = $ctx->var($args->{name}) || ''; |
|
72
|
|
|
|
|
|
|
} else { |
|
73
|
0
|
|
|
|
|
|
$args->{tag} =~ s/^$PREFIX//; |
|
74
|
0
|
|
|
|
|
|
my $handler = $ctx->handler_for($args->{tag}); |
|
75
|
0
|
|
|
|
|
|
$ctx->stash('tag', $args->{tag}); |
|
76
|
0
|
0
|
|
|
|
|
if (defined($handler)) { |
|
77
|
0
|
|
|
|
|
|
my $value = $handler->($ctx, {%$args}); |
|
78
|
0
|
0
|
0
|
|
|
|
if (defined($value) && $value ne '') { # want to include "0" here |
|
79
|
0
|
|
|
|
|
|
$val = $ctx->pass_tokens($ctx, $args, $cond); |
|
80
|
|
|
|
|
|
|
} else { |
|
81
|
0
|
|
|
|
|
|
$val = $ctx->pass_tokens_else($ctx, undef, $cond); |
|
82
|
|
|
|
|
|
|
} |
|
83
|
|
|
|
|
|
|
} else { |
|
84
|
0
|
|
|
|
|
|
$val = $ctx->pass_tokens_else($ctx); |
|
85
|
|
|
|
|
|
|
} |
|
86
|
0
|
|
|
|
|
|
$ctx->unstash('tag'); |
|
87
|
|
|
|
|
|
|
} |
|
88
|
0
|
0
|
|
|
|
|
if ($tag eq 'Unless') { |
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return !$val; |
|
90
|
|
|
|
|
|
|
} elsif ($tag eq 'UnlessEmpty') { |
|
91
|
0
|
|
|
|
|
|
return $val ne ''; |
|
92
|
|
|
|
|
|
|
} elsif ($tag eq 'UnlessZero') { |
|
93
|
0
|
|
|
|
|
|
return $val != 0; |
|
94
|
|
|
|
|
|
|
} elsif ($tag eq 'IfOne') { |
|
95
|
0
|
|
|
|
|
|
return $val == 1; |
|
96
|
|
|
|
|
|
|
} else { # If |
|
97
|
0
|
|
|
|
|
|
return $val; |
|
98
|
|
|
|
|
|
|
} |
|
99
|
|
|
|
|
|
|
} |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
sub include { # file only. option to run through builder??? IncludeModule? |
|
102
|
0
|
|
|
0
|
0
|
|
my ($arg, $cond) = @_[1, 2]; |
|
103
|
0
|
|
|
|
|
|
my $file = $arg->{file}; |
|
104
|
0
|
|
|
|
|
|
require File::Spec; |
|
105
|
0
|
|
|
|
|
|
my @paths = |
|
106
|
|
|
|
|
|
|
($file, map File::Spec::catfile($_, $file), Typist->instance->tmpl_path); |
|
107
|
0
|
|
|
|
|
|
my $path; |
|
108
|
0
|
|
|
|
|
|
for my $p (@paths) { |
|
109
|
0
|
0
|
0
|
|
|
|
$path = $p, last if -e $p && -r _; |
|
110
|
|
|
|
|
|
|
} |
|
111
|
0
|
0
|
|
|
|
|
return $_[0] |
|
112
|
|
|
|
|
|
|
->error(Typist->translate("Can't find included file '[_1]'", $path)) |
|
113
|
|
|
|
|
|
|
unless $path; |
|
114
|
0
|
|
|
|
|
|
local *FH; |
|
115
|
0
|
0
|
|
|
|
|
open FH, $path |
|
116
|
|
|
|
|
|
|
or return |
|
117
|
|
|
|
|
|
|
$_[0]->error( |
|
118
|
|
|
|
|
|
|
Typist->translate( |
|
119
|
|
|
|
|
|
|
"Error opening included file '[_1]': [_2]", $path, $! |
|
120
|
|
|
|
|
|
|
) |
|
121
|
|
|
|
|
|
|
); |
|
122
|
0
|
|
|
|
|
|
my $c = ''; |
|
123
|
0
|
|
|
|
|
|
local $/; |
|
124
|
0
|
|
|
|
|
|
$c = ; |
|
125
|
0
|
|
|
|
|
|
close FH; |
|
126
|
0
|
|
|
|
|
|
$c; |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head1 NAME |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Typist::Template::Tag - Standard template tags plugin |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 TAGS |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
NOTE: These tags will be prefixed with the value from |
|
138
|
|
|
|
|
|
|
C in the instance of L. By default this is |
|
139
|
|
|
|
|
|
|
'MT' |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=over |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item SetVar |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item GetVar |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=item SetVarBlock |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item If |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=item Unless |
|
152
|
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item IfOne |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=item UnlessEmpty |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item UnlessZero |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item Else |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item Include |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=item Ignore |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=back |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
=head1 TO DO |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
Add |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
How does this connect up with the stash or vars though? |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=end |