> If I were to have statement in my template such as...
> 
> [% INCLUDE foo
>     title = 'Hello World'
>     size = 35
>     offset = 10
> %]
> 
> 
> Is there a way for the file/block 'foo' to know which variables I have
> passed it? (somewhat akin to @_ for perl functions).  Or perhaps 
> phrased another way, can 'foo' access it's stash?

There is a related issue.  I don't know of a way for foo to distinguish
between being called as

    [% INCLUDE foo title = 'Hello World' %]

versus an extraneous, unexpected, earlier setting of an argument
variable, eg:

    [%# snip %]
    [% title = 'Some other text' %]
    [%# snip %]
    [% INCLUDE foo %]

ie: if foo takes an optional argument I don't know how to protect from
someone inadventently setting the variable prior to including foo.

I vaguely recall Andy has some ideas for fixing this in v3, although
I can't find it in the design document.  The proposed DO and STASH
directives are relevant.  See http://www.template-toolkit.org/v3/design.html.

Anyhow, for V2 you could do this:

    [% INCLUDE foo
         args.title = 'Hello World'
         args.size = 35
         args.offset = 10
    %]

or:

    [% INCLUDE foo
        args = {
           title = 'Hello World',
           size = 35,
           offset = 10,
        }
    %]

Then foo gets a hash called args with all the arguments, and is welcome
to loop over args.keys or whatever.

The first example doesn't protect the case of someone setting elements of
args earlier. To be pedantic you could write the first form as:

    [% args = {}; INCLUDE foo
         args.title = 'Hello World'
         args.size = 35
         args.offset = 10
    %]

but the second example is cleaner.

Craig


Reply via email to