[htmltmpl] Nested templates
Brought to you by:
samtregar
|
From: Brad B. <bm...@ma...> - 2009-11-25 02:24:56
|
Hi all,
I once tried to implement nested templates with HTML::Template.
Below is an example of what I came up with:
1 #!/usr/local/bin/perl
2
3 use strict;
4 use warnings;
5 use HTML::Template;
6
7 my $text = <<__;
8 This is a <TMPL_VAR NAME="color"> bird.
9 This is a <TMPL_VAR NAME="<TMPL_VAR NAME="color">"> bird.
10 This is a <TMPL_VAR NAME="<TMPL_VAR NAME="<TMPL_VAR
NAME="color">">"> bird.
11 __
12
13 my $output = '';
14 my $sref = \$text;
15 my $count;
16
17 while (1) {
18 $count++;
19
20 my $tmpl = HTML::Template->new(
21 scalarref => $sref,
22 strict => 0,
23 die_on_bad_params => 0,
24 case_sensitive => 1,
25 );
26
27 $tmpl->param( color => 'blue' );
28 $tmpl->param( blue => 'BLUE' );
29 $tmpl->param( BLUE => '**BLUE**' );
30
31 $text = $tmpl->output();
32
33 print "\n$count:\n$text"; # debug
34
35 last if $output eq $text; # no changes since last time
36 $output = $text;
37
38 }
39
40 print "\nfinal: \n$output";
41
42 __END__
1:
This is a blue bird.
This is a <TMPL_VAR NAME="blue"> bird.
This is a <TMPL_VAR NAME="<TMPL_VAR NAME="blue">"> bird.
2:
This is a blue bird.
This is a BLUE bird.
This is a <TMPL_VAR NAME="BLUE"> bird.
3:
This is a blue bird.
This is a BLUE bird.
This is a **BLUE** bird.
4:
This is a blue bird.
This is a BLUE bird.
This is a **BLUE** bird.
final:
This is a blue bird.
This is a BLUE bird.
This is a **BLUE** bird.
I would like a better way of doing this. The calls to
new() on line 20, the copies on lines 31 and 36, and
the compares on line 36 just seem all a bit much for
what ought (I think) to be more like
1 while s/from/to/g;
Is there a better way?
Apologies if this has been beat to death in the past ...
Regards,
Brad
|