On Tue, 24 Nov 2009 21:24:30 -0500, Brad Baxter wrote
> 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?

I would just do:

#!/usr/local/bin/perl

use strict;
use warnings;
use HTML::Template;

my $template = qq|
This is a <tmpl_var color> bird.
This is a <tmpl_if is_blue>BLUE</tmpl_if> bird.
This is a <tmpl_if is_blue>**BLUE**</tmpl_if> bird.
|;

# all the logic of what the color will be goes here
my $color = 'blue';

my $tmpl = HTML::Template->new(
   scalarref         => \$template,
   strict            => 0,
   die_on_bad_params => 0,
   case_sensitive    => 1,
);

$tmpl->param(
               color   => $color,
               is_blue => $color eq 'blue' ? 1 : 0,
            );

print $tmpl->output();

So many advantages. The logic is completely separate from the presentation. If 
you send the template
to a speaker of a different language, they can localize that with no problems. 
The code is much
shorter at 29 lines vs. 42 lines, and its much more clear to a future 
maintainer what is happening
without any brain gymnastics.

HTH,
Alex

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Html-template-users mailing list
Html-template-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to