简体   繁体   中英

how to call a subroutine from perl catalyst template

I would like to know whether its possible to call a subroutine from a template in Perl catalyst and also pass values to that subroutine.

subrountine:

sub get_ceil : Private{
    my ( $self, $c, $param ) = @_;
    my $value =  ceil($param);
    $c->stash->{ceil} = $value;
}

Template: [% ceil = $c->forward('/vbo/my_goals/mortgage_reduction_program/get_ceil',[])%]

Please let me know

我认为您不能。只能从Model调用子例程。

[% Catalyst.model('xxx').yyy('param1','param2') %]

Going through the Catalyst dispatch process for the example provided seems like overkill. It is quite straightforward to do what you require, either by creating a Template plugin to handle the ceil function, or through the Template USE var = Class(module) construct.

Either ultimately produces more or less the same effect.

=== Template/Plugin/MyFunc.pm ===

package Template::Plugin::MyFunc;
use base 'Template::Plugin';
our $VERSION = 0.01;

use strict;
use warnings;

sub new {
    my ($class, $context) = @_;
    bless {
        _CONTEXT => $context,
    }, $class;
}

sub ceil {
    my ($self, $param) = @_;
    return ceil($param)
}

sub other_func {
    ... etc
}

1;

=== some template ===

[% USE MyFunc %]

<td>[% MyFunc.ceil(some_param) %]</td>

I haven't researched it, but I'd be surprised if there weren't Template plugins to do functions like ceil() , floor() and so on already out there on CPAN.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM