如何将参数传递给模板工具包函数中的子例程
How to pass arguments to a subroutine in template toolkit function
我的文档foo.tt我想这样写:
[% INCLUDE header('str', 1, 2, 3, 5, 10) %]
我的目标是对 str 进行一些字符串操作,然后使用 foreach / for 遍历所有数字。
不幸的是,我无法在模板工具包中找到这种类型的语法。
模板工具包将参数传递给子例程的方式是什么?
有什么办法吗?
你可以传递参数,但你需要给它们命名。示例:
outer.tt2:
[% INCLUDE header.tt2 header_string="str", items=[ 1, 2, 3, 5, 10 ] -%]
header.tt2:
String: [% header_string %]
[% FOREACH item IN items -%]
Item: [% item %]
[% END -%]
输出:
String: str
Item: 1
Item: 2
Item: 3
Item: 5
Item: 10
查看 MACRO
定义:
[% MACRO header(str, items) BLOCK -%]
[% FOREACH i IN items; -%]
... your item code here ...
[% END -%]
[% END -%]
[% header('str',[1, 2, 3, 5, 10]) %]
如果 TT 在模板级别公开原始参数列表,您可以按照您的指示调用它(例如 header('str', 1, 2, 3, 5, 10)
),但这非常接近。
我的文档foo.tt我想这样写:
[% INCLUDE header('str', 1, 2, 3, 5, 10) %]
我的目标是对 str 进行一些字符串操作,然后使用 foreach / for 遍历所有数字。
不幸的是,我无法在模板工具包中找到这种类型的语法。
模板工具包将参数传递给子例程的方式是什么?
有什么办法吗?
你可以传递参数,但你需要给它们命名。示例:
outer.tt2:
[% INCLUDE header.tt2 header_string="str", items=[ 1, 2, 3, 5, 10 ] -%]
header.tt2:
String: [% header_string %]
[% FOREACH item IN items -%]
Item: [% item %]
[% END -%]
输出:
String: str
Item: 1
Item: 2
Item: 3
Item: 5
Item: 10
查看 MACRO
定义:
[% MACRO header(str, items) BLOCK -%]
[% FOREACH i IN items; -%]
... your item code here ...
[% END -%]
[% END -%]
[% header('str',[1, 2, 3, 5, 10]) %]
如果 TT 在模板级别公开原始参数列表,您可以按照您的指示调用它(例如 header('str', 1, 2, 3, 5, 10)
),但这非常接近。