Re: [Templates] TT/Locale::Maketext problem

[prev] [thread] [next] [Date index for 2004/06/08]

From: Chris Winters
Subject: Re: [Templates] TT/Locale::Maketext problem
Date: 23:34 on 08 Jun 2004
On Jun 8, 2004, at 12:35 PM, Karthik Krishnamurthy wrote:
> ...
> In the template something of the form
>
> [% IF error %]
>       <h3 class="errorwarning">
> [% lang.maketext(error.msg, error.args) %]
>       </h3>
> [% END %]
>
> This works if there is only one argument involved. If there are two I 
> get
>
> ARRAY(0x8d84058) already forwarded to. The original error message was 
> of the
> form "[_1] is already forwarded to [_2]" ? Can anybody tell me what I 
> am doing
> wrong ?

IIRC, TT helpfully squishes lists returned from methods/subroutines 
into list references if there is more than one returned value. Here's 
an example:

----------
package MyFoo;

sub new { return bless( {}, $_[0] ) }

sub mycall {
     my ( $self, @args ) = @_;
     return "Passed: " .join( ', ', @args );
}

sub getarg { return ( 'single-arg' ) }

sub getargs { return ( 'this', 'is', 'a list' ) }


package main;

use Template;

my $template = Template->new();
$template->process( \*DATA, { foo => MyFoo->new() } )
     || die "Cannot process: ", $template->error();

__DATA__
First:  [% foo.mycall( 'a' ) %]
Second: [% foo.mycall( foo.getarg ) %]
Third:  [% foo.mycall( foo.getargs ) %]
----------

This will return

----------
First:  Passed: a
Second: Passed: single-arg
Third:  Passed: ARRAY(0x86623c)
----------

To get around it you should pass a wrapper routine to your template in 
place of the 'lang' variable. For instance:

----------
my %params = (
     MSG => sub {
         my ( $key, $args ) = @_;
         my @msg_args = ( ref $args eq 'ARRAY' ) ? @{ $args } : ( $args 
);
         return $lang->maketext( $key, @msg_args );
    },
    ...
);
$template->process( 'foo.txt', \%params )
----------

Centralizing this is a piece of cake.

Good luck,

Chris

--
Chris Winters
Creating enterprise-capable snack systems since 1988


_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] TT/Locale::Maketext problem
Karthik Krishnamurthy 16:35 on 08 Jun 2004

Re: [Templates] TT/Locale::Maketext problem
Chris Winters 23:34 on 08 Jun 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52