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;