本文介绍了在没有插件的情况下调用模板工具包中的外部子模块和模块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Template Toolkit .tt文件中调用外部Perl模块.我要使用的模块是Util,我想调用Util::prettify_date.我能够使用Template Toolkit的插件界面包含此模块:我设置了load,new和error函数(如下所述: http://template-toolkit.org/docs/modules/Template/Plugin.html ),并使用[% USE Util %]将其包含在内.

I am trying to call an outside Perl module in a Template Toolkit .tt file. The module I want to use is Util, and I want to call Util::prettify_date. I was able to include this module using Template Toolkit's plugin interface: I set up the load, new, and error function (as described here: http://template-toolkit.org/docs/modules/Template/Plugin.html), and include it using [% USE Util %].

这很好用,但是我想知道是否有一种方法可以在Template Toolkit中使用USE Perl模块而不必对其进行插件化.制作插件的主要问题是,我必须在面向对象的Util中制作所有函数(即,将$ self作为第一个参数),这没有任何意义.

This works fine, but I was wondering if there's a way to USE Perl modules in Template Toolkit without having to plugin-ify them. The main issue with making plugins is that I have to make all the functions in Util object-oriented (ie. accepts $self as first argument), which doesn't really make sense.

推荐答案

您是否尝试过在 [% PERL %] 块?

Have you tried useing the module in a [% PERL %] block?

现在,我个人将编写一个插件,该插件在摆脱第一个参数之后将MyOrg::Plugin::Util->prettify_date中继到Util::prettify_date.您也可以自动创建这些方法:

Now, I personally would write a plugin which relays, say, a MyOrg::Plugin::Util->prettify_date to Util::prettify_date after getting rid of the first argument. You can automate the creation of these methods as well:

my @to_proxy = qw( prettify_date );

sub new {
    my $class = shift;

    {
        no strict 'refs';
        for my $sub ( @to_proxy) {
            *{"${class}::${sub}"} = sub {
                my $self = shift;
                return "My::Util::$sub"->( @_ );
            }
        }
    }
    bless {} => $class;
}

这篇关于在没有插件的情况下调用模板工具包中的外部子模块和模块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 13:59