在没有插件的情况下调用模板工具包中的外部子程序和模块?
我试图在 Template Toolkit .tt 文件中调用外部 Perl 模块。 我想使用的模块是Util
,我想调用Util::prettify_date
。 我能够使用 Template Toolkit 的插件接口包含此模块:我设置了加载、新建和错误函数(如下所述:http://template-toolkit.org/docs/modules/Template/Plugin.html),并使用 [% USE Util %].
这工作正常,但我想知道是否有一种方法可以在 Template Toolkit 中使用
Perl 模块,而无需对它们进行插件化。 制作插件的主要问题是我必须使 Util
中的所有函数都面向对象(即接受 $self 作为第一个参数),这实际上没有意义。
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 %]
.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以将函数(即子例程)传递给模板,如下所示:
not_plugin.tt
将产生这个:
You can also pass functions (ie. subroutines) to template like this:
not_plugin.tt
will produce this:
您是否尝试过在 < 中
使用
该模块code>[% PERL %] 块?现在,我个人会编写一个插件,在摆脱第一个后将
MyOrg::Plugin::Util->prettify_date
中继到Util::prettify_date
争论。 您也可以自动创建这些方法:Have you tried
use
ing the module in a[% PERL %]
block?Now, I personally would write a plugin which relays, say, a
MyOrg::Plugin::Util->prettify_date
toUtil::prettify_date
after getting rid of the first argument. You can automate the creation of these methods as well:实现此目的的最简单、最危险的方法是使用
[% PERL %]
块并强制在main
命名空间中进行计算。这是必要的,因为
[% PERL %]
块是 在隔离的Template::Perl
包命名空间中进行评估,您将使用package main
覆盖该命名空间。危险来自于您的模板能够
The simplest, most dangerous way to accomplish this is to use a
[% PERL %]
block and force the evaluation to occur in themain
namespace.This is necessary because the
[% PERL %]
block is evaluated in an isolatedTemplate::Perl
package namespace, which you are overriding withpackage main
The danger comes from your template being able to write into the main namespace in addition to reading from it, which can lead to some interesting debug.