Frederic_S has asked for the wisdom of the Perl Monks concerning the following question:

Hallo there once again!

This time around, I have a question concerning the template toolkit. Perhaps someone here knows a good lot about this tool.

I would like to combine variables in an extension I'm programming for my company for Bugzilla. Here's the explenation:

# Due to having to iterate through a lot of values, but also needing t +o perform further calculations, I must save the results of the iterat +ion like this: $UserInfo->{"RemainingDays".$_."-".$UtilRatioPercent} = $RemainingTime +; # $_ can either be NEW or ASSIGNED $UtilRatioPercent is variable. # The hash in which this is written is transferred to the template lik +e so: $vars->{'ReturnValues'} = \@ReturnValues; # And then processed in the template as follows: [% FOREACH TimeData IN ReturnValues %] [% TimeData.RemainingDaysNEW-XX %] [% END %]
I need to insert $UtilRatioPercent where XX is and have the template read the whole thing as ONE variable which would then look like this for example:

[% TimeData.RemainingDaysNEW-75 %]
Anybody know how I can achieve this? The template toolkit documentation doesn't really help and I can't access newsgroups from my companies office.

Regards

Fred

Replies are listed 'Best First'.
Re: Perl Template::Toolkit combining variables?
by Anonyrnous Monk (Hermit) on Jan 05, 2011 at 14:21 UTC

    Is there any specific reason you need to squeeze everything into a single hash key/template variable?  Why not create a proper nested data structure, which you can then access something like this in the template

    [% TimeData.RemainingDays.$status.$percent %]

    To elaborate, here's some sample code:

    #!/usr/bin/perl -w use strict; use Template; my $vars; $vars->{Stati} = [ qw(NEW ASSIGNED) ]; $vars->{UtilRatioPercent} = [ qw(25 50 75) ]; for (1..3) { my $item; for my $status (@{$vars->{Stati}}) { for my $percent (@{$vars->{UtilRatioPercent}}) { $item->{RemainingDays}{$status}{$percent} = int(rand 42); } } push @{ $vars->{ReturnValues} }, $item; } #use Data::Dumper; print Dumper $vars; exit; # debug my $templ = q{ [% FOREACH status IN Stati %] [% FOREACH percent IN UtilRatioPercent %] [% FOREACH TimeData IN ReturnValues %] Remaining ([% status %]-[% percent %]): [% TimeData.RemainingDay +s.$status.$percent %] days [% END %] [% END %] [% END %] }; my $tt = Template->new(); $tt->process(\$templ, $vars) or die $tt->error();

    Output (superfluous whitespace removed):

    Remaining (NEW-25): 0 days Remaining (NEW-25): 29 days Remaining (NEW-25): 11 days Remaining (NEW-50): 29 days Remaining (NEW-50): 4 days Remaining (NEW-50): 14 days Remaining (NEW-75): 23 days Remaining (NEW-75): 23 days Remaining (NEW-75): 27 days Remaining (ASSIGNED-25): 3 days Remaining (ASSIGNED-25): 5 days Remaining (ASSIGNED-25): 13 days Remaining (ASSIGNED-50): 3 days Remaining (ASSIGNED-50): 23 days Remaining (ASSIGNED-50): 3 days Remaining (ASSIGNED-75): 14 days Remaining (ASSIGNED-75): 1 days Remaining (ASSIGNED-75): 37 days
Re: Perl Template::Toolkit combining variables?
by duelafn (Parson) on Jan 05, 2011 at 13:58 UTC

    If I understand your needs correctly, You can use variables as key names, but, I believe, need to store the full key value to a variable. Something like the following:

    [% when = 'NEW'; num = 75; key = 'RemainingDays' _ when _ '-' _ num; %] ... [% TimeData.$key %]

    Good Day,
        Dean

      Thanks Dean,

      that was exactly what I was looking for.

      Hell of alot of work to implement, but it was worth it!

      Now, the project admin can enter different utilization ratios for the programmers on the team and bug deadlines are calculated on the fly according to estimated work hours and util ratio and shown directly in Bugzilla - totally dynamic :-D

      Thank you for your help!

      Regards,

      Fred
Re: Perl Template::Toolkit combining variables?
by jethro (Monsignor) on Jan 05, 2011 at 13:13 UTC

    I don't understand your requirements, both the why and the what is a mystery to me. For example: Why do you have to use this data structure? If the data structure is not suitable for the templating system you can just generate a second temporary suitable data structure out of the data, call the template and throw the temporary data away.

    But even with your actual data it seems you just need a nested loop to access the values:

    [% FOREACH TimeData IN ReturnValues %] [% FOREACH Rd IN TimeData %] [% TimeData.Rd %] [% END %] [% END %]

    Disclaimer: I'm not an TT expert

      Hey Jethro, what I want or better need to do is a concatenation of two variables in the template. Usually you concatenate two variables with a "." as in:

      $Variable1."-".$Variable2;
      Output:

      Value1-Value2

      This doesn't work in the template.

      Depending on what utilization ratio a programmer has, a certain value needs to be output in the template. This is why I need to make the template variable dynamic.

      The first part of the variable is:

      [% TimeData.RemainingTimeNEW- OR [% TimeData.RemainingTimeASSIGNED-
      Now I need to concatenate the second part of the variable.

      [% TimeData.RemainingTimeNEW-UtilRatioPercent %]
      I tried the following solutions but with no luck:

      [% TimeData.RemainingTimeNEW-_UtilRatioPercent %] [% TimeData.RemainingTimeNEW-$UtilRatioPercent %] [% TimeData.RemainingTimeNEW-${UtilRatioPercent} %] [% TimeData.RemainingTimeNEW-.UtilRatioPercent %]
      The variable should be read out as:

      TimeData.RemainingTimeNEW-75

      Where the 75 is variable

      Hope this sheds some light on what I want.

      Regards,

      Fred