in reply to Text::Template=>How to fill in with more than one time?

All you have to do is call fill_in more than once, using the same template object each time. Did you try it?
  • Comment on Re: Text::Template=>How to fill in with more than one time?

Replies are listed 'Best First'.
Re: Re: Text::Template=>How to fill in with more than one time?
by taizica (Novice) on Sep 04, 2003 at 06:01 UTC
    Hi there:

    Yes, I tried it. The filled variables in the template in the first call became null in the second call of fill_in. The template only got the value of variables used in the second call of fill_in.

    Here is the template


    It has come to our attention that you are delinquent in your {$monthname[$last_paid_month]} payment. Please remit ${sprintf("%.2f", $amount)} immediately, or your patellae may be needlessly endangered. Love, Mark "Vizopteryx" Dominus

    Here is the script

    #!/usr/bin/perl5.6 use Text::Template; my $template = Text::Template->new(SOURCE => 'formletter.tmpl') or die "Couldn't construct template: $Text::Template::ERROR" +; my @monthname = qw(January February March April May June July August September October November Dece +mber); my %vars1 = (title => 'Mr.', last_paid_month => 1, # February amount => 392.12, monthname => \@monthname, ); my $result = $template->fill_in(HASH => \%vars1); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" } my %vars2 = (title => 'Mr.', firstname => 'Bill', lastname => 'Gates', ); $result = $template->fill_in(HASH => \%vars2); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" }

    Here is the output

    Dear Mr. ,
    It has come to our attention that you are delinquent in your
    February payment. Please remit
    $392.12 immediately, or your patellae may be needlessly endangered.
    Love,
    Mark "Vizopteryx" Dominus
    Dear Mr. Gates,
    It has come to our attention that you are delinquent in your
    payment. Please remit
    $0.00 immediately, or your patellae may
    be needlessly endangered.
    Love,
    Mark "Vizopteryx" Dominus

    <edit: castaway: added code tags>
      Argh!
      I played around with this for a while before I realised the mistake.. Your set of values for the second template contains neither 'last_paid_month', nor 'amount', the other values aren't even being used.. It's no wonder theres no month or value in the second output..

      C.

      (Well that was a good waste of 20 minutes.. Do I feel stupid..)

      Use another hash to hold the variables and just keep adding your new hash to it. Anything you change will override the previous values, anything you don't will just stay there.
      #!/usr/bin/perl5.6 use Text::Template; my $template = Text::Template->new(SOURCE => 'formletter.tmpl') or die "Couldn't construct template: $Text::Template::ERROR" +; my @monthname = qw(January February March April May June July August September October November Dece +mber); my %vars1 = (title => 'Mr.', last_paid_month => 1, # February amount => 392.12, monthname => \@monthname, ); my %keepvars = %vars1; my $result = $template->fill_in(HASH => \%keepvars); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" } my %vars2 = (title => 'Mr.', firstname => 'Bill', lastname => 'Gates', ); %keepvars = (%keepvars, %vars2); $result = $template->fill_in(HASH => \%keepvars); if (defined $result) { print $result } else { die "Couldn't fill in template: $Text::Template::ERROR" }
        Thanks to all for your very helpful responses.

        My intention was to fill in a template by diiferent hash tables since my template will be big with a lot of variables to be defined. It looks like that I have no choice but just fill-in the template after collected all the varaibles in one shoot.

        I really hope that the Text::Template could allow people to save previous filled-in data, i.e. do accumulation and also allow people to "refresh" if needed. This may not make sense?

        Regards
        Taizi
        Instead of:
        %keepvars = (%keepvars, %vars2);
        You could also do this:
        @keepvars{keys %vars2} = values %vars2;
      Oh, now I understand: you want to call it more than once but have the variables from the first call still be there, right? I thought you just wanted to produce multiple different results without loading the template again.

      I don't understand why you would want to do that, but the answer is no, you can't. Instead, you should collect all the values for a single run of the template before you call fill_in. Is there a reason you are avoiding that?