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];
53 use Storable qw(dclone);
55 use Debbugs::Config qw(:config);
57 use Params::Validate qw(:types validate_with);
62 =head2 fill_in_template
64 print fill_in_template(template => 'template_name',
65 variables => \%variables,
69 Reads a template from disk (if it hasn't already been read in) and
70 fills the template in.
75 our %filled_templates;
79 my %param = validate_with(params => \@_,
80 spec => {template => SCALAR|HANDLE|SCALARREF,
81 variables => {type => HASHREF,
84 language => {type => SCALAR,
87 output => {type => HANDLE,
90 safe => {type => OBJECT,
95 return _fill_in_template(@param{qw(template variables language safe output)});
100 my $template = shift;
101 $filled_templates{$template}++;
102 print STDERR "include template $template language $language safe $safe\n" if $DEBUG;
103 # Die if we're in a template loop
104 die "Template loop with $template" if $filled_templates{$template} > 10;
107 $filled_tmpl = Debbugs::Text::_fill_in_template($template,
116 print STDERR "failed to fill template $template: $@";
118 print STDERR "failed to fill template $template\n" if $filled_tmpl eq '' and $DEBUG;
119 print STDERR "template $template '$filled_tmpl'\n" if $DEBUG;
120 $filled_templates{$template}--;
124 sub _fill_in_template{
126 @param{qw(template variables language safe output nosafe)} = @_;
127 print STDERR "_fill template $param{template} language $param{language} safe $param{safe}\n"
133 if (ref($param{template}) eq 'GLOB' or
134 ref(\$param{template}) eq 'GLOB') {
135 $tt_type = 'FILE_HANDLE';
136 $tt_source = $param{template};
138 elsif (ref($param{template}) eq 'SCALAR') {
140 $tt_source = ${$param{template}};
144 $tt_source = _locate_text($param{template},$param{language});
146 if (not defined $tt_source) {
147 die "Unable to find template $param{template} with language $param{language}";
150 if (defined $param{safe}) {
151 $safe = $param{safe};
154 print STDERR "Created new safe\n" if $DEBUG;
155 $safe = Safe->new() or die "Unable to create safe compartment";
157 my @modules = ('Text::Template' => undef,
158 # This doesn't work yet; have to figure it out
159 #'Debbugs::Config' => [qw(:globals :config)],
161 while (my ($module,$param) = splice (@modules,0,2)) {
162 print STDERR "Eval $module\n" if $DEBUG;
164 if (not defined $param) {
165 $code = "use $module;";
168 $code = "use $module ".(join(',',map {"q($_)"} @{$param})).';';
171 print STDERR "Error while attempting to eval '$code': $@" if $@;
173 $safe->permit_only(':base_core',':base_io',':base_mem',':base_loop',
174 qw(padsv padav padhv padany),
175 qw(rv2gv refgen srefgen ref),
177 $safe->share('$language','%tt_templates','$safe','$variables','%filled_templates');
178 $safe->share('*STDERR');
179 $safe->share('&_fill_in_template');
180 $safe->share('%config');
181 $safe->share('&include');
182 my $root = $safe->root();
183 # load variables into the safe
184 for my $key (keys %{$param{variables}||{}}) {
185 print STDERR "Loading $key\n" if $DEBUG;
186 if (ref($param{variables}{$key})) {
188 print STDERR $safe->root().'::'.$key,qq(\n) if $DEBUG;
189 *{"${root}::$key"} = $param{variables}{$key};
193 ${"${root}::$key"} = $param{variables}{$key};
198 # perldoc Opcode; for details
199 $language = $param{language};
201 if ($tt_type eq 'FILE' and
202 defined $tt_templates{$tt_source} and
203 (stat $tt_source)[9] > $tt_templates{$tt_source}{mtime}
205 $tt = $tt_templates{$tt_source}{template};
208 if ($tt_type eq 'FILE') {
209 $tt_templates{$tt_source}{mtime} =
210 (stat $tt_source)[9];
212 $tt = Text::Template->new(TYPE => $tt_type,
213 SOURCE => $tt_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(#(defined $param{nosafe} and $param{nosafe})?():(HASH=>$param{variables}),
223 (defined $param{nosafe} and $param{nosafe})?():(SAFE=>$safe),
225 (defined $param{nosafe} and $param{nosafe})?(PACKAGE => 'main'):(),
226 defined $param{output}?(OUTPUT=>$param{output}):(),
228 if (not defined $ret) {
229 print STDERR $Text::Template::ERROR;
234 no warnings 'uninitialized';
235 my $temp = $param{nosafe}?'main':$safe->{Root};
236 print STDERR "Variables for $param{template}\n";
237 print STDERR "Safe $temp\n";
238 print STDERR map {"$_:${$_}\n"} keys %{"${temp}::"};
239 print STDERR ${"${temp}::search_value"},qq(\n);
246 my ($template,$language) = @_;
247 $template =~ s/\.tmpl$//g;
248 # if a language doesn't exist, use the en_US template
249 if (not -e $config{template_dir}.'/'.$language.'/'.$template.'.tmpl') {
252 my $loc = $config{template_dir}.'/'.$language.'/'.$template.'.tmpl';
254 print STDERR "Unable to locate template $loc\n";