[英]How to differentiate between false and defined values in Template Toolkit for perl
This is a question about Template Toolkit for perl.这是一个关于 Perl 模板工具包的问题。
I render my templates with a little command-line utility that has the following option enabled我使用一个启用了以下选项的小命令行实用程序来呈现我的模板
DEBUG => Template::Constants::DEBUG_UNDEF,
The syntax is render <file.tt> var1 val1 var2 val2 ....
This is very convenient because the user gets prompts about values that need to be defined, for example语法是
render <file.tt> var1 val1 var2 val2 ....
这很方便,因为用户会得到需要定义的值的提示,例如
$ render file.tt
undef error - var1 is undefined
$ render file.tt var1 foo
undef error - var2 is undefined
$ render file.tt var1 foo var2 bar
... template renders correctly
For some (optional) values, templates provide defaults, for example:对于某些(可选)值,模板提供默认值,例如:
[%- DEFAULT
hostname = 0
%]
Then the template body would typically contain:那么模板主体通常会包含:
[% IF hostname %] hostname = [% hostname %][% ELSE %][% -- a comment, variable hostname not provided %][% END %]
How do I make the above idiom work for variables where 0
is a valid value?如何使上述习语适用于
0
是有效值的变量?
I want the following to happen:我希望发生以下情况:
render template.tt
Template renders:模板渲染:
-- this is a comment, variable enable_networking not provided
For为了
render template.tt enable_networking 0
I want我想要
enable_networking = 0
The problem is differentiating between defined values and false values.问题是区分定义的值和错误的值。 I have tried using
-1
(instead of 0
) both in the DEFAULT
block and in the [% IF enable_networking == -1 %]
statement.我尝试在
DEFAULT
块和[% IF enable_networking == -1 %]
语句中使用-1
(而不是0
)。
However, the following DEFAULT
block但是,以下
DEFAULT
块
[% DEFAULT enable_networking = -1 %]
will override value 0
specified on the command-line.将覆盖在命令行上指定的值
0
。 (It sees a enable_networking
is false and sets it to -1) (它看到
enable_networking
为假并将其设置为 -1)
Are there any easy work-arounds (some config variable maybe?)是否有任何简单的解决方法(可能是一些配置变量?)
To check if a variable is undefined or not you could check if its size
method returns something greater than 0. Of course this case only aplies if the variable is not initialized or defined at all ( enable_networking = ''
has size = 1, the same with enable_networking = 0
)要检查变量是否未定义,您可以检查其
size
方法是否返回大于 0 的值。当然,这种情况仅适用于变量未初始化或根本未定义的情况( enable_networking = ''
大小为 1,相同使用enable_networking = 0
)
To check if a variable is not false... well... first you have to describe wich type of value is false.要检查一个变量是否不假......好吧......首先你必须描述哪个类型的值是假的。
In this case i will take size = 0 (or size doesn't exists) as undefined, -1 as false and everything else as true:在这种情况下,我将 size = 0(或 size 不存在)视为未定义,-1 为假,其他一切为真:
[% IF enable_networking.size and enable_networking != -1 %]
enable_networking = [% enable_networking %]
[% ELSE %]
-- a comment, variable enable_networking not provided
[% END %]
With the code above if you run使用上面的代码,如果你运行
render template.tt enable_networking 0
The output will be enable_networking = 0
输出将为
enable_networking = 0
And if you run如果你跑
render template.tt
The output will be -- a comment, variable enable_networking not provided
even if you don't declare [% DEFAULT enable_networking = -1 %]
输出将是
-- a comment, variable enable_networking not provided
即使您不声明[% DEFAULT enable_networking = -1 %]
-- a comment, variable enable_networking not provided
EDIT 1:编辑 1:
The length
method is better suited for this job: length
方法更适合这项工作:
[% IF enable_networking.length and enable_networking != -1 %]
enable_networking = [% enable_networking %]
[% ELSE %]
-- a comment, variable enable_networking not provided
[% END %]
Using length
instead of size
also allows you to use enable_networking = ''
as FALSE along with -1使用
length
而不是size
还允许您使用enable_networking = ''
as FALSE 和 -1
EDIT 2:编辑2:
Ok, after the comments i found a workaround that do the trick: the TRY
- CATCH
directives...好的,在评论之后,我找到了一种解决方法:
TRY
- CATCH
指令...
For optional variables that can have value of 0 the objective would be to TRY
setting the variable value to itself, if the variable is defined the value will be assigned, otherwise we CATCH
the undef error and set the default value.对于值可以为 0 的可选变量,目标是
TRY
将变量值设置为自身,如果定义了变量,则将分配该值,否则我们CATCH
undef 错误并设置默认值。 For any other type of variable we can use the DEFAULT
directive:对于任何其他类型的变量,我们可以使用
DEFAULT
指令:
[% DEFAULT
hostname = 0
%]
[% TRY %] [% enable_networking = enable_networking %] [% CATCH %] [% enable_networking = -1; %] [% END %]
hostname = [% hostname %]
[% IF enable_networking != -1 AND enable_networking.length %] enable_networking = [% enable_networking %][% ELSE %]-- variable enable_networking not provided[% END %]
With this new template if you run如果你运行这个新模板
$ render template.tt
hostname = 0
-- variable enable_networking not provided
$ render template.tt enable_networking ""
hostname = 0
-- variable enable_networking not provided
$ render template.tt hostname myhost enable_networking 0
hostname = myhost
enable_networking = 0
All scalar values in TT have a .defined
vmethod. TT 中的所有标量值都有一个
.defined
vmethod。
[% IF hostname.defined %] hostname = [% hostname %][% ELSE %][% -- a comment, variable hostname not provided %][% END %]
This is discussed in the manual . 手册中对此进行了讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.