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];
52 use Storable qw(dclone);
54 use Debbugs::Config qw(:config);
56 use Params::Validate qw(:types validate_with);
62 our %filled_templates;
65 # This function is what is called when someone does include('foo/bar')
66 # {include('foo/bar')}
70 $filled_templates{$template}++;
71 print STDERR "include template $template language $language\n" if $DEBUG;
72 # Die if we're in a template loop
73 die "Template loop with $template" if $filled_templates{$template} > 10;
76 $filled_tmpl = fill_in_template(template => $template,
78 language => $language,
82 print STDERR "failed to fill template $template: $@";
84 print STDERR "failed to fill template $template\n" if $filled_tmpl eq '' and $DEBUG;
85 print STDERR "template $template '$filled_tmpl'\n" if $DEBUG;
86 $filled_templates{$template}--;
91 =head2 fill_in_template
93 print fill_in_template(template => 'template_name',
94 variables => \%variables,
98 Reads a template from disk (if it hasn't already been read in) and
99 fills the template in.
104 sub fill_in_template{
105 my %param = validate_with(params => \@_,
106 spec => {template => SCALAR|HANDLE|SCALARREF,
107 variables => {type => HASHREF,
110 language => {type => SCALAR,
113 output => {type => HANDLE,
116 safe => {type => OBJECT|UNDEF,
119 hole_var => {type => HASHREF,
125 print STDERR "fill_in_template ";
126 print STDERR join(" ",map {exists $param{$_}?"$_:$param{$_}":()} keys %param);
133 if (ref($param{template}) eq 'GLOB' or
134 ref(\$param{template}) eq 'GLOB') {
135 $tt_type = 'FILE_HANDLE';
136 $tt_source = $param{template};
137 binmode($tt_source,":encoding(UTF-8)");
139 elsif (ref($param{template}) eq 'SCALAR') {
141 $tt_source = ${$param{template}};
145 $tt_source = _locate_text($param{template},$param{language});
147 if (not defined $tt_source) {
148 die "Unable to find template $param{template} with language $param{language}";
151 $language = $param{language};
153 if ($tt_type eq 'FILE' and
154 defined $tt_templates{$tt_source} and
155 ($tt_templates{$tt_source}{mtime} + 60) < time and
156 (stat $tt_source)[9] <= $tt_templates{$tt_source}{mtime}
158 $tt = $tt_templates{$tt_source}{template};
161 my $passed_source = $tt_source;
162 my $passed_type = $tt_type;
163 if ($tt_type eq 'FILE') {
164 $tt_templates{$tt_source}{mtime} =
165 (stat $tt_source)[9];
166 $passed_source = IO::File->new($tt_source,'r');
167 binmode($passed_source,":encoding(UTF-8)");
168 $passed_type = 'FILEHANDLE';
170 $tt = Text::Template->new(TYPE => $passed_type,
171 SOURCE => $passed_source,
174 if ($tt_type eq 'FILE') {
175 $tt_templates{$tt_source}{template} = $tt;
178 if (not defined $tt) {
179 die "Unable to create Text::Template for $tt_type:$tt_source";
181 my $ret = $tt->fill_in(PACKAGE => 'DTT',
182 HASH => {%{$param{variables}//{}},
183 (map {my $t = $_; $t =~ s/^\&//; ($t => $param{hole_var}{$_})}
184 keys %{$param{hole_var}//{}}),
185 include => \&Debbugs::Text::include,
188 defined $param{output}?(OUTPUT=>$param{output}):(),
190 if (not defined $ret) {
191 print STDERR $Text::Template::ERROR;
196 no warnings 'uninitialized';
197 print STDERR "Variables for $param{template}\n";
204 my ($template,$language) = @_;
205 $template =~ s/\.tmpl$//g;
206 # if a language doesn't exist, use the en_US template
207 if (not -e $config{template_dir}.'/'.$language.'/'.$template.'.tmpl') {
210 my $loc = $config{template_dir}.'/'.$language.'/'.$template.'.tmpl';
212 print STDERR "Unable to locate template $loc\n";