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 base qw(Exporter);
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;
67 our $hole = Safe::Hole->new({});
70 # This function is what is called when someone does include('foo/bar')
71 # {include('foo/bar')}
75 $filled_templates{$template}++;
76 print STDERR "include template $template language $language safe $safe\n" if $DEBUG;
77 # Die if we're in a template loop
78 die "Template loop with $template" if $filled_templates{$template} > 10;
81 $filled_tmpl = fill_in_template(template => $template,
83 language => $language,
88 print STDERR "failed to fill template $template: $@";
90 print STDERR "failed to fill template $template\n" if $filled_tmpl eq '' and $DEBUG;
91 print STDERR "template $template '$filled_tmpl'\n" if $DEBUG;
92 $filled_templates{$template}--;
97 =head2 fill_in_template
99 print fill_in_template(template => 'template_name',
100 variables => \%variables,
104 Reads a template from disk (if it hasn't already been read in) and
105 fills the template in.
110 sub fill_in_template{
111 my %param = validate_with(params => \@_,
112 spec => {template => SCALAR|HANDLE|SCALARREF,
113 variables => {type => HASHREF,
116 language => {type => SCALAR,
119 output => {type => HANDLE,
122 safe => {type => OBJECT|UNDEF,
125 hole_var => {type => HASHREF,
130 #@param{qw(template variables language safe output hole_var no_safe)} = @_;
132 print STDERR "fill_in_template ";
133 print STDERR join(" ",map {exists $param{$_}?"$_:$param{$_}":()} keys %param);
140 if (ref($param{template}) eq 'GLOB' or
141 ref(\$param{template}) eq 'GLOB') {
142 $tt_type = 'FILE_HANDLE';
143 $tt_source = $param{template};
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 (stat $tt_source)[9] <= $tt_templates{$tt_source}{mtime}
198 $tt = $tt_templates{$tt_source}{template};
201 if ($tt_type eq 'FILE') {
202 $tt_templates{$tt_source}{mtime} =
203 (stat $tt_source)[9];
205 $tt = Text::Template->new(TYPE => $tt_type,
206 SOURCE => $tt_source,
209 if ($tt_type eq 'FILE') {
210 $tt_templates{$tt_source}{template} = $tt;
213 if (not defined $tt) {
214 die "Unable to create Text::Template for $tt_type:$tt_source";
216 my $ret = $tt->fill_in(#SAFE => $safe,
218 HASH => {%{$param{variables}//{}},
219 (map {my $t = $_; $t =~ s/^\&//; ($t => $param{hole_var}{$_})}
220 keys %{$param{hole_var}//{}}),
221 include => \&Debbugs::Text::include,
224 defined $param{output}?(OUTPUT=>$param{output}):(),
226 if (not defined $ret) {
227 print STDERR $Text::Template::ERROR;
232 no warnings 'uninitialized';
233 # my $temp = $param{nosafe}?'main':$safe->{Root};
234 print STDERR "Variables for $param{template}\n";
235 # print STDERR "Safe $temp\n";
236 # print STDERR map {"$_: ".*{$_}."\n"} keys %{"${temp}::"};
243 my ($template,$language) = @_;
244 $template =~ s/\.tmpl$//g;
245 # if a language doesn't exist, use the en_US template
246 if (not -e $config{template_dir}.'/'.$language.'/'.$template.'.tmpl') {
249 my $loc = $config{template_dir}.'/'.$language.'/'.$template.'.tmpl';
251 print STDERR "Unable to locate template $loc\n";