如何将 undef 作为参数从 TT 模板传递给对象方法?

发布于 2024-08-10 00:48:23 字数 535 浏览 6 评论 0原文

Template-Toolkit 似乎总是希望将 undef 插入空字符串。因此,像这样的模板:

Result is [% some_object.some_method (1, undef, 2) %]

或这样:

Result is [% ttvar %]
          [% some_object.some_method (1, ttvar, 2) %]

生成对 Perl 的调用,例如:

some_object->some_method (1, '', 2)

当我想要的是:

some_object->some_method (1, undef, 2)

有没有办法传递 undef 而不是空字符串?

Template-Toolkit seems to want to always interpolate undef to the empty string. So a template like this:

Result is [% some_object.some_method (1, undef, 2) %]

or this:

Result is [% ttvar %]
          [% some_object.some_method (1, ttvar, 2) %]

produces a call to Perl like:

some_object->some_method (1, '', 2)

when what I want is:

some_object->some_method (1, undef, 2)

Is there any way to pass undef instead of an empty string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

街道布景 2024-08-17 00:48:23

我添加了另一个答案来展示 EVAL_PERL 如何在 TT 中工作的示例:

use Template;
use DateTime;

my $tt = Template->new( EVAL_PERL => 1 );

my $vars = { foo => 'DateTime', bar => DateTime->now, p => 'print' };

my $file = q{
    [% SET hello = 'Hello world' %]
    [% PERL %]
    print "[% hello %]\n";
    print [% foo %]->now, "\n";
    [% p %] $stash->get( 'bar' )->ymd;
    [% END %]
};

$tt->process( \$file, $vars );

上面的输出如下:

Hello world
2009-11-03T15:31:50
2009-11-03

因为 TT 充当预处理器并生成以下要解释的 Perl 代码:

print "hello world\n";
print DateTime->now, "\n";
print $stash->get( 'bar' )->ymd;

注意。上面一行中的 $stash 由 TT 提供,是对顶级存储对象的引用。

/I3az/

I've added another answer to show an example of how EVAL_PERL works in TT:

use Template;
use DateTime;

my $tt = Template->new( EVAL_PERL => 1 );

my $vars = { foo => 'DateTime', bar => DateTime->now, p => 'print' };

my $file = q{
    [% SET hello = 'Hello world' %]
    [% PERL %]
    print "[% hello %]\n";
    print [% foo %]->now, "\n";
    [% p %] $stash->get( 'bar' )->ymd;
    [% END %]
};

$tt->process( \$file, $vars );

The above outputs the following:

Hello world
2009-11-03T15:31:50
2009-11-03

Because TT is acting as a pre-processor and produces the following Perl code to interpret:

print "hello world\n";
print DateTime->now, "\n";
print $stash->get( 'bar' )->ymd;

NB. $stash in above line is provided by TT and is a reference to the top level stash object.

/I3az/

少女的英雄梦 2024-08-17 00:48:23

使用[% PERL %]怎么样?

[% PERL %]
[% my_perl_code %]
[% END %]

How about using [% PERL %]?

[% PERL %]
[% my_perl_code %]
[% END %]
神也荒唐 2024-08-17 00:48:23

这是 Template Toolkit 的设计决策。 Perl 模板工具包“Badger book” 第 50 页:

如果模板工具包遇到未定义值的变量,它不会抱怨。相反,它会悄悄地使用一个空字符串(即什么都没有)作为变量的值,并继续处理模板的提醒。

然而,您可以做的是使用 DEBUG 选项让 TT 在看到 undef 时提供警告。请参阅SO问题Perl的模板工具包可以对未定义的值发出警告吗? 了解更多信息。

/I3az/

This is a design decision with Template Toolkit. From page 50 of the Perl Template Toolkit "Badger book":

The Template Toolkit won't complain if it encounters a variable for which it doesn't have a value defined. Instead, it will quietly use an empty string (i.e., nothing at all) for the value of the variable and continue to process the reminder of the template.

However what you can do is make TT provide a warning when it sees an undef by using the DEBUG option. See the SO question Can Perl’s Template Toolkit warn on undefined values? for more info.

/I3az/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文