perltemplate-toolkit

How to pass arguments to a subroutine in template toolkit function


I my document foo.tt i would like to write something like this:

[% INCLUDE header('str', 1, 2, 3, 5, 10) %]

My aim is to do some string manipulation on str and then with foreach / for iterates over all the numbers.

unfortunately i was unable to find that this type of syntax in Template toolkit is allowed.

What is Template toolkit way for passing arguments to a subroutine?

Any ideas how to do it?


Solution

  • You can pass arguments, but you need to give them names. Example:

    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 -%]
    

    output:

    String: str
    Item: 1
    Item: 2
    Item: 3
    Item: 5
    Item: 10