line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::Meta::Interpolate; |
2
|
|
|
|
|
|
|
#============================================================================= |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# AUTHOR |
5
|
|
|
|
|
|
|
# Jonas Liljegren |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# COPYRIGHT |
8
|
|
|
|
|
|
|
# Copyright (C) 2004-2009 Jonas Liljegren. All Rights Reserved. |
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# This module is free software; you can redistribute it and/or |
11
|
|
|
|
|
|
|
# modify it under the same terms as Perl itself. |
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
#============================================================================= |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Template::Plugin::Meta::Interpolate - Allow evaluation of META parameters |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
25598
|
use 5.010; |
|
1
|
|
|
|
|
3
|
|
22
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
20
|
|
23
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
37
|
|
24
|
1
|
|
|
1
|
|
4
|
use base "Template::Plugin"; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2065
|
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 SYNOPSIS |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
[% USE Meta::Interpolate %] |
31
|
|
|
|
|
|
|
[% META title = '-"${user.username} at Our Site"' %] |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Show username in title |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
[% META title='-"404 - " _ loc("File not found")' %] |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
The value is enclosed in C<''>. The initial C<-> tells us to |
38
|
|
|
|
|
|
|
interpolate the rest as if it said: |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
[% title = "404 - " _ loc("File not found") %] |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
The C<_> is a concatenation operator. And L is |
43
|
|
|
|
|
|
|
the translation function. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
It is common to wrap Template Toolkit templates in a base template, then use the meta function to set a page tittle. |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
[% META title = 'Book List' %] |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Unfortunately the tittle can only contain static text. This plugin allow you to also use variables and expressions. |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
If value starts with a '-', the rest of value will be evaluated as a TT |
55
|
|
|
|
|
|
|
expression. (No expressions are normally allowed in META.) |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
If value starts with a '~', the rest of value will be evaluated as a |
58
|
|
|
|
|
|
|
TT string with variable interpolation. It's the same as for '-' but |
59
|
|
|
|
|
|
|
with the extra "" around the value. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
It does not sets the variable if it's already true. That enables you |
62
|
|
|
|
|
|
|
to set it in another way. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 Example |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
[* META otitle = '-otitle=["[_1]s administration pages", site.name]' *] |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
The normal use is to set the variable with the result of the template |
69
|
|
|
|
|
|
|
output. But to give complex values to a variable, like a list, you can |
70
|
|
|
|
|
|
|
do it as above. |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 In wrapper |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
In your wrapper set your title like this. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
[% template.title or "My site name!" %] |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=cut |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
############################################################################## |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub new |
85
|
|
|
|
|
|
|
{ |
86
|
0
|
|
|
0
|
1
|
|
my( $self, $context, @params ) = @_; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# warn "new Meta::Interpolate\n"; |
89
|
0
|
|
|
|
|
|
my $cfg = $context->config; |
90
|
0
|
|
0
|
|
|
|
my $st = $cfg->{START_TAG} || '[%'; |
91
|
0
|
|
|
|
|
|
$st =~ s/\\//g; |
92
|
0
|
|
0
|
|
|
|
my $et = $cfg->{END_TAG} || '%]'; |
93
|
0
|
|
|
|
|
|
$et =~ s/\\//g; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
my $stash = $context->stash; |
96
|
0
|
|
|
|
|
|
my $template = $stash->{'template'}; |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
|
|
|
foreach my $key (keys %{$template}) |
|
0
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
{ |
100
|
0
|
0
|
|
|
|
|
next if $key =~ /^_/; |
101
|
0
|
|
|
|
|
|
my $val = $template->{$key}; |
102
|
0
|
0
|
|
|
|
|
if( $val =~ /^-(.*)/s ) |
|
|
0
|
|
|
|
|
|
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
|
|
|
my $src = $st.' '.$1.' '.$et; |
105
|
|
|
|
|
|
|
# warn "Parsing $template->{$key} => $src\n"; |
106
|
0
|
|
|
|
|
|
$val = $context->process( \$src, {} ); |
107
|
|
|
|
|
|
|
# warn "Got value $val\n"; |
108
|
0
|
0
|
|
|
|
|
unless( $stash->get($key) ) |
109
|
|
|
|
|
|
|
{ |
110
|
|
|
|
|
|
|
# warn "Assigning val to $key\n"; |
111
|
0
|
|
|
|
|
|
$stash->set($key, $val); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
elsif( $val =~ /^~(.*)/s ) |
115
|
|
|
|
|
|
|
{ |
116
|
0
|
|
|
|
|
|
my $src = $st.' "'.$1.'" '.$et; |
117
|
|
|
|
|
|
|
# warn "Parsing $template->{$key} => $src\n"; |
118
|
0
|
|
|
|
|
|
$val = $context->process( \$src, {} ); |
119
|
0
|
0
|
|
|
|
|
unless( $stash->get($key) ) |
120
|
|
|
|
|
|
|
{ |
121
|
0
|
|
|
|
|
|
$stash->set($key, $val); |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
else |
125
|
|
|
|
|
|
|
{ |
126
|
0
|
|
|
|
|
|
$stash->set($key, $val); |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
return $self; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
############################################################################## |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 AUTHOR |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Jonas Liljegren |
140
|
|
|
|
|
|
|
jonas@paranormal.se |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 COPYRIGHT |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Copyright (C) 2004-2009 Jonas Liljegren. All Rights Reserved. |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or |
147
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head1 CPAN maintainer |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Runar Buvik |
152
|
|
|
|
|
|
|
CPAN ID: RUNARB |
153
|
|
|
|
|
|
|
runarb@gmail.com |
154
|
|
|
|
|
|
|
http://www.runarb.com |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=head1 Git |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
L |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=head1 SEE ALSO |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
L |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=cut |