【发布时间】:2021-09-27 11:18:32
【问题描述】:
我想定义一个带有少量参数的模板工具包宏,如果在该位置没有给出参数,则至少有一个带有默认值。可能吗? 我的想法是有这样的东西(类似于 Python 语法/逻辑):
[%-
MACRO my_macro( arg1, arg2, arg3='my_default_value') BLOCK;
# arg3 value is 'my_default_value' if nothing is passed as argument in that position, otherwise it must use arg3 value given when called.
END;
-%]
然后调用宏:
# In this case, arg3 inside the macro must be 'my_default_value'
my_macro('my_value1', 'my_value2');
# In this case, arg3 inside the macro must be 'my_value3'
my_macro('my_value1', 'my_value2', 'my_value3');
【问题讨论】:
-
也许您可以使用宏块开头的
PERL指令来修改存储?如果未将 stash 变量作为参数给出,它将被设置为空字符串,因此您可以执行类似my $arg3 = $stash->get('arg3'); $arg3 = 'my_default_value' if $arg3 eq ""; $stash->set(arg3 => $arg3)的操作
标签: perl template-toolkit