Template Toolkit virtual methods with less mess
Template Toolkit has a very cool feature called Virtual Methods. It provide methods on native Perl5 data types.
An example is this (In TT syntax):
Since I like to use TT from CGI::Application and I don't like to actually see its guts. I choose to find a way to add a vmethod without the unpretty $Template::Stash::LIST_OPS->{} syntax. I think that is quite unfriendly to TT noobs like most of my coworkers.
I found that define_vmethod is available off the TT Context object. This is available with $tt->context or in my case off plugin TT object in CGI App $self->tt_obj->context.
The parameters are context->define_vmethod( $type, $name, \&implementation )
[% IF indexes.$rate.value.abs > 10 %]
Here is the implementation:
First parameter for each type:
An example is this (In TT syntax):
[% listy = [ 47, -47, 42, -55, 4, -24 ] %]Often its called autoboxing. Well TT allows you to define your own. The details are available in the TT VMethod Manual. In that page there is a TODO about a method called define_vmethod.
[% listy.size %]
[% listy.join(', ') %]
Since I like to use TT from CGI::Application and I don't like to actually see its guts. I choose to find a way to add a vmethod without the unpretty $Template::Stash::LIST_OPS->{} syntax. I think that is quite unfriendly to TT noobs like most of my coworkers.
I found that define_vmethod is available off the TT Context object. This is available with $tt->context or in my case off plugin TT object in CGI App $self->tt_obj->context.
The parameters are context->define_vmethod( $type, $name, \&implementation )
- $type - data type, 'scalar', 'list', 'array' or 'hash'
- $name - the name for you method 'abs', 'smash', 'love'
- \&implementation - sub reference that does the method
[% IF indexes.$rate.value.abs > 10 %]
Here is the implementation:
## inside my CGI::App run modeThe parameters passed to the sub reference depend upon the datatype. But after the data type value then additional parameters are passed, so I could have added parameters to the abs call. E.g.
sub qa {
my $self = shift; ## grab cgi::app object
$self->tt_obj->context->define_vmethod( 'scalar', 'abs',
sub { return abs( shift ) } );
....
}
[% indexes.$name.value.abs(10) %]In that case the parameters would have been: my @_ = ( $list_value, 10 );
First parameter for each type:
- scalar - value
- list||array - reference to list
- hash - reference to hash
Thank you for sharing this information.
ReplyDeleteWe are facing constant growing of data amount in both the Web and personal computers. And the more information we need to proceed the more time it takes. That is why I think that it's worth it to know virtual data room pricing and make a decision.
I am just such a person luxurious online video slot machines come in and win
ReplyDeleteTreasureBox is operated by a group of young, passionate, and ambitious people that are working diligently towards the same goal - make your every dollar count, as we believe you deserve something better.
ReplyDeletesofa bed
shoe rack nz