Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've been using the Template module for quite a while now and have had no problems, I can figure out most things on my own. In fact, this is the first time I'm unable to solve this by myself and it's frustrating me :). I've got the following array (with real data, not typed numbers) and am passing it to Template as shown.:

my $options = [ qw(one two three) ]; $TMPL->process('somewhere.tmpl', { myopts => $options } );

Okay, so that part is trivial. Now, within the template (somewhere.tmpl in the example), I am using a [% PROCESS another.tmpl %]. Now, this other template is expecting an array of hashes in a variable called 'options'. Each one of the options needs to be passed along in the 'value' key of the hash. Since that probably doesn't make much sense in words, here's some perl pseudo-code for what I need to accomplish in TT syntax:

[% PROCESS "another.tmpl" options = [ map { {value => $_} } myopts ] %]

I've checked the TT docs and I can't find a map() within TT. There is a grep virtual method, but no map. How do I go about passing this barrier? It's proving to be more difficult to me than you'd think! I thank thee brother or sister ahead of time for your wonderful wisdom.

Replies are listed 'Best First'.
•Re: Juggling structures with Template Toolkit
by merlyn (Sage) on Apr 02, 2004 at 21:48 UTC
    Well, that's bordering on a little too much processing in TT code. But, you can brute force it:
    [% # untested and I may get it wrong... sorry newdata = []; FOR item = myopts; newdata.push({ 'value' => item }); END; PROCESS another.tmpl options => newdata; %]

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Okay, that makes sense. Thank you.

      that's bordering on a little too much processing in TT code

      How would you go about doing that processing within the script then? I did try something along the lines of the following snippet and it failed horribly. Perhaps I'm just a little off?

      $TMPL->process('something.tmpl', { myopts => [ map { {value => $_} } qw(one two three) ] } ); __END__ [% PROCESS "another.tmpl" options => myopts %]
        That works perfectly for me, except that it looks like you should have assigned that array to "options" instead of "myopts" or maybe both.

        update: I will now admit that I am officially dumbed out for the day. The script method with the map works fine. I don't know what happened earlier. Perhaps I forgot to double up the curly braces around the map. Thanks to merlyn for showing a TT way of doing it anyhow :)

Re: Juggling structures with Template Toolkit
by perrin (Chancellor) on Apr 02, 2004 at 21:55 UTC
    Is this what you mean?

    [% FOREACH option = myopts %] [% PROCESS another.tmpl value = option %] [% END %]

      No, I need a method to dynamically generate this:

      [% PROCESS "another.tmpl" options => [ { value => 'one' }, { value => 'two' }, { value => 'three' } ] %]