[Templates] passing positional arguments to MACROs

Sean McAfee eefacm at gmail.com
Mon Mar 17 17:37:20 GMT 2008


On Mon, Mar 17, 2008 at 6:14 AM, Thomas Klausner <domm at cpan.org> wrote:

> Hi!
>
> I'm currently trying to pass some arbitrary values (basically a list)
> through a macro:
>
> [% MACRO mt(msg) GET c.maketext(msg) %]
>
> [% mt("Hello %1", c.user.name %]
> [% mt("%1 does %2", 'foo', 'bar' %]
>
> The problem ist that the above macro definition does not work like this.
> Using named parameters seems cumbersom. One alternative would be to pass
> the params in as an arrayref:
>
> [% mt("%1 does %2", ['foo', 'bar'] %]
>
> but what I'd actually love to do is something like:
>
> [% MACRO mt(msg, at _) GET c.maketext(msg, at _) %]
>
> is this possible somehow?
>

>From examining the source code generated by the MACRO directive: No, sorry.

I don't think the named parameters need to be too cumbersome.  You could do
something like this:

[% mt("%1 does %2", _1="foo", _2="bar") %]

...And then have c.maketext get its input fields from the stash, rather than
from its arguments.  But then, I guess, you'd have to be very careful to
always supply all of the parameters, lest a variable leak in from an outer
scope.

If that's still too convoluted, the array reference is probably your best
bet.

Hmmm... Or, I suppose one could subclass Template::Directive to provide a
MACRO implementation that processes its arguments more to one's liking.
That could be fun.


--Sean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.template-toolkit.org/pipermail/templates/attachments/20080317/84130236/attachment.htm 


More information about the templates mailing list