FWIW, below is my final version of code to fill
in nested templates.  It's down to a call to new()
and a call to output() in each loop (besides the
pattern match).

Of course, there's a hidden param operation via
the assoc object.  Whether or not this benchmarks
better, I don't know, but it comes across as
simpler IMO.

Of course, it introduces a bit of a dummy class
for the associate option, but I don't mind that
too much.

Thanks all,

Brad


     1  #!/usr/local/bin/perl
     2
     3  use strict;
     4  use warnings;
     5  use HTML::Template;
     6
     7  my $assoc = My::Param->new(
     8      { color => 'blue', blue  => 'BLUE', BLUE  => '**BLUE**' }
     9      );
    10
    11  my $text = <<__;
    12  This is a <TMPL_VAR NAME="color"> bird.
    13  This is a <TMPL_VAR NAME="<TMPL_VAR NAME="color">"> bird.
    14  This is a <TMPL_VAR NAME="<TMPL_VAR NAME="<TMPL_VAR
NAME="color">">"> bird.
    15  __
    16
    17  my $sref = \$text;
    18  my $count;
    19
    20  while ( $text =~ /<TMPL_/i ) {
    21
    22      my $tmpl = HTML::Template->new(
    23          scalarref         => $sref,
    24          strict            => 0,
    25          case_sensitive    => 1,
    26          associate         => $assoc,
    27          );
    28
    29      $text = $tmpl->output();
    30
    31      print "\n".++$count.":\n$text";  # debug
    32  }
    33
    34  print "\nfinal: \n$text";
    35
    36  package My::Param;
    37  sub new   { bless $_[1], $_[0] }
    38  sub param { my $self = shift;
    39      return keys %$self if wantarray;
    40      return $self->{ $_[0] } }
    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.

final:
This is a blue bird.
This is a BLUE bird.
This is a **BLUE** bird.

------------------------------------------------------------------------------
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