Skip Menu |

This queue is for tickets about the HTML-Template-Pluggable CPAN distribution.

Report information
The Basics
Id: 40714
Status: resolved
Priority: 0/
Queue: HTML-Template-Pluggable

People
Owner: Nobody in particular
Requestors: MADZ [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.17
Fixed in: (no value)



Subject: Calling methods doesn't seem to work
I cant make method calls work, such as <tmpl_var name="myobject.method(somevar)"> To verify the problem, I have rewritten the example used in the documentation and found that it isn't working either. For now, I have been unable to trigger method calls using parameters: #!/usr/bin/perl use Test::More tests => 3; use HTML::Template::Pluggable; use HTML::Template::Plugin::Dot; use Number::Format; sub render { my( $template, %vars ) = @_; my $result; eval { my $t = new HTML::Template::Pluggable( scalarref => \$template ); while(my( $key, $value ) = each( %vars )) { $t->param( $key => $value ); } $result = $t->output; }; diag $@ if $@; return $result; } my $out; $out = render( "Amount: <tmpl_var order.total_amount>", order => { total_amount => 12.2 } ); ok( $out eq 'Amount: 12.2', 'Substitute value from hashref' ); my $formatter = new Number::Format; ok( $formatter && $formatter->isa('Number::Format'), 'Instantiate Number::Format' ); $out = render( "Amount: <tmpl_var Formatter.format_currency('US',order.total_amount)>", Formatter => $formatter, order => { total_amount => 12.2 } ); diag $out; ok( $out eq 'attribute_value' ); 1; Produces: 1..3 ok 1 - Substitute value from hashref ok 2 - Instantiate Number::Format # HTML::Template : Attempt to set nonexistent parameter 'order' - this parameter name doesn't match any declarations in the template file : die_on_bad_params => 1) at test-nf.pl line 15 # undef not ok 3 # Failed test at test-nf.pl line 42. # Looks like you failed 1 test of 3.
Subject: test-nf.pl
#!/usr/bin/perl use Test::More tests => 3; use HTML::Template::Pluggable; use HTML::Template::Plugin::Dot; use Number::Format; sub render { my( $template, %vars ) = @_; my $result; eval { my $t = new HTML::Template::Pluggable( scalarref => \$template ); while(my( $key, $value ) = each( %vars )) { $t->param( $key => $value ); } $result = $t->output; }; diag $@ if $@; return $result; } my $out; $out = render( "Amount: <tmpl_var order.total_amount>", order => { total_amount => 12.2 } ); ok( $out eq 'Amount: 12.2', 'Substitute value from hashref' ); my $formatter = new Number::Format; ok( $formatter && $formatter->isa('Number::Format'), 'Instantiate Number::Format' ); $out = render( "Amount: <tmpl_var Formatter.format_currency('US',order.total_amount)>", Formatter => $formatter, order => { total_amount => 12.2 } ); diag $out; ok( $out eq 'attribute_value' ); 1;
Subject: test.pl
#!/usr/bin/perl use Test::More tests => 4; use HTML::Template::Pluggable; use HTML::Template::Plugin::Dot; use Carp 'croak'; sub render { my( $template, %vars ) = @_; my $t = new HTML::Template::Pluggable( scalarref => \$template ); while(my( $key, $value ) = each( %vars )) { eval { $t->param( $key => $value ) }; diag $@ if $@; } return $t->output; } my $out; $out = render( "<tmpl_var testobj.attribute>", testobj => new testclass() ); ok( $out eq 'attribute_value' ); $out = render( "<tmpl_var testobj.hello>", testobj => new testclass() ); ok( $out eq 'hello' ); $out = render( "<tmpl_var testobj.echo('1')>", testobj => new testclass() ); ok( $out eq '1' ); $out = render( "<tmpl_var testobj.echo(somevar)>", testobj => new testclass(), somevar => 'somevalue' ); ok( $out eq 'somevalue' ); $out = render( "<tmpl_var name=\"testobj.test(somevar)\">", testobj => new testclass(), somevar => 'somevalue' ); ok( $out eq 'somevalue' ); $out = render( "<tmpl_var name='somevar'><tmpl_var testobj.echo(somevar)>", testobj => new testclass(), somevar => 'somevalue' ); ok( $out eq 'somevaluesomevalue' ); $out = render( "<tmpl_var name='somevar'>", testobj => new testclass(), somevar => 'somevalue' ); ok( $out eq 'somevalue' ); package testclass; sub new { my $class = shift; my $self = { attribute => 'attribute_value' }; return bless $self, $class; } sub echo { shift; return join(', ', @_); } sub hello { return 'hello'; } 1;
On Thu Nov 06 06:01:07 2008, MADZ wrote:
Show quoted text
> I cant make method calls work, such as
>
> <tmpl_var name="myobject.method(somevar)">

That isn't just any method call, it's actually a compound statement where "somevar" is another template parameter.
The reason it fails for you is that you don't supply both "myobject" and "somevar" at the same time. Here's how this fails:

1. suppose you first call $template->param( somevar => $somevalue );
    that fails, because the template doesn't have a <tmpl_var somevar>
2. suppose you first call $template->param( myobject => $someobject );
    then we can't compute the input to "method", because we don't have "somevar" yet

The only way to do this is by passing both params at the same time:
  $template->param( 
      myobject => $someobject,
      somevar => $somevalue,
  );

Rhesa