1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later
3 # version at your option.
4 # See the file README and COPYING for more information.
6 # Copyright 2007 by Don Armstrong <don@donarmstrong.com>.
15 Debbugs::Text -- General routines for text templates
19 use Debbugs::Text qw(:templates);
20 print fill_in_template(template => 'cgi/foo');
24 This module is a replacement for parts of common.pl; subroutines in
25 common.pl will be gradually phased out and replaced with equivalent
26 (or better) functionality here.
35 use vars qw($DEBUG $VERSION @EXPORT_OK %EXPORT_TAGS @EXPORT @ISA);
36 use Exporter qw(import);
40 $DEBUG = 0 unless defined $DEBUG;
43 %EXPORT_TAGS = (templates => [qw(fill_in_template)],
46 Exporter::export_ok_tags(qw(templates));
47 $EXPORT_TAGS{all} = [@EXPORT_OK];
48 push @ISA,qw(Safe::Hole::User);
55 use Storable qw(dclone);
57 use Debbugs::Config qw(:config);
59 use Params::Validate qw(:types validate_with);
65 our %filled_templates;
69 # This function is what is called when someone does include('foo/bar')
70 # {include('foo/bar')}
74 $filled_templates{$template}++;
75 print STDERR "include template $template language $language safe $safe\n" if $DEBUG;
76 # Die if we're in a template loop
77 die "Template loop with $template" if $filled_templates{$template} > 10;
80 $filled_tmpl = fill_in_template(template => $template,
82 language => $language,
87 print STDERR "failed to fill template $template: $@";
89 print STDERR "failed to fill template $template\n" if $filled_tmpl eq '' and $DEBUG;
90 print STDERR "template $template '$filled_tmpl'\n" if $DEBUG;
91 $filled_templates{$template}--;
96 =head2 fill_in_template
98 print fill_in_template(template => 'template_name',
99 variables => \%variables,
103 Reads a template from disk (if it hasn't already been read in) and
104 fills the template in.
109 sub fill_in_template{
110 my %param = validate_with(params => \@_,
111 spec => {template => SCALAR|HANDLE|SCALARREF,
112 variables => {type => HASHREF,
115 language => {type => SCALAR,
118 output => {type => HANDLE,
121 safe => {type => OBJECT|UNDEF,
124 hole_var => {type => HASHREF,
129 #@param{qw(template variables language safe output hole_var no_safe)} = @_;
131 print STDERR "fill_in_template ";
132 print STDERR join(" ",map {exists $param{$_}?"$_:$param{$_}":()} keys %param);
139 if (ref($param{template}) eq 'GLOB' or
140 ref(\$param{template}) eq 'GLOB') {
141 $tt_type = 'FILE_HANDLE';
142 $tt_source = $param{template};
143 binmode($tt_source,":encoding(UTF-8)");
145 elsif (ref($param{template}) eq 'SCALAR') {
147 $tt_source = ${$param{template}};
151 $tt_source = _locate_text($param{template},$param{language});
153 if (not defined $tt_source) {
154 die "Unable to find template $param{template} with language $param{language}";
157 # if (defined $param{safe}) {
158 # $safe = $param{safe};
161 # print STDERR "Created new safe\n" if $DEBUG;
162 # $safe = Safe->new() or die "Unable to create safe compartment";
163 # $safe->permit_only(':base_core',':base_loop',':base_mem',
164 # qw(padsv padav padhv padany),
165 # qw(rv2gv refgen srefgen ref),
166 # qw(caller require entereval),
167 # qw(gmtime time sprintf prtf),
170 # $safe->share('*STDERR');
171 # $safe->share('%config');
172 # $hole->wrap(\&Debbugs::Text::include,$safe,'&include');
173 # my $root = $safe->root();
174 # # load variables into the safe
175 # for my $key (keys %{$param{variables}||{}}) {
176 # print STDERR "Loading $key\n" if $DEBUG;
177 # if (ref($param{variables}{$key})) {
179 # print STDERR $safe->root().'::'.$key,qq(\n) if $DEBUG;
180 # *{"${root}::$key"} = $param{variables}{$key};
184 # ${"${root}::$key"} = $param{variables}{$key};
187 # for my $key (keys %{exists $param{hole_var}?$param{hole_var}:{}}) {
188 # print STDERR "Wraping $key as $param{hole_var}{$key}\n" if $DEBUG;
189 # $hole->wrap($param{hole_var}{$key},$safe,$key);
192 $language = $param{language};
194 if ($tt_type eq 'FILE' and
195 defined $tt_templates{$tt_source} and
196 ($tt_templates{$tt_source}{mtime} + 60) < time and
197 (stat $tt_source)[9] <= $tt_templates{$tt_source}{mtime}
199 $tt = $tt_templates{$tt_source}{template};
202 my $passed_source = $tt_source;
203 my $passed_type = $tt_type;
204 if ($tt_type eq 'FILE') {
205 $tt_templates{$tt_source}{mtime} =
206 (stat $tt_source)[9];
207 $passed_source = IO::File->new($tt_source,'r');
208 binmode($passed_source,":encoding(UTF-8)");
209 $passed_type = 'FILEHANDLE';
211 $tt = Text::Template->new(TYPE => $passed_type,
212 SOURCE => $passed_source,
215 if ($tt_type eq 'FILE') {
216 $tt_templates{$tt_source}{template} = $tt;
219 if (not defined $tt) {
220 die "Unable to create Text::Template for $tt_type:$tt_source";
222 my $ret = $tt->fill_in(#SAFE => $safe,
224 HASH => {%{$param{variables}//{}},
225 (map {my $t = $_; $t =~ s/^\&//; ($t => $param{hole_var}{$_})}
226 keys %{$param{hole_var}//{}}),
227 include => \&Debbugs::Text::include,
230 defined $param{output}?(OUTPUT=>$param{output}):(),
232 if (not defined $ret) {
233 print STDERR $Text::Template::ERROR;
238 no warnings 'uninitialized';
239 # my $temp = $param{nosafe}?'main':$safe->{Root};
240 print STDERR "Variables for $param{template}\n";
241 # print STDERR "Safe $temp\n";
242 # print STDERR map {"$_: ".*{$_}."\n"} keys %{"${temp}::"};
249 my ($template,$language) = @_;
250 $template =~ s/\.tmpl$//g;
251 # if a language doesn't exist, use the en_US template
252 if (not -e $config{template_dir}.'/'.$language.'/'.$template.'.tmpl') {
255 my $loc = $config{template_dir}.'/'.$language.'/'.$template.'.tmpl';
257 print STDERR "Unable to locate template $loc\n";