Ask Bj?rn Hansen wrote:
> One of them is an idiom like this:
> 
> Foo: <% join "--", map { $_->name } $beer->types %><br />

Yes, that would be nice.  

> With TT I end up with something like the following to do the same:
> 
> [% jobs = [];
>    FOR j = company.Jobs;
>      str = '<a href="/job/' _ j.ID _ '">' _ j.Title;
>      jobs.push(str);
>    END %]
> Blah: [% jobs.join(", ") %]<br />
> 
> Did I miss something obvious?

Nope, not really, but there is at least one hack which can make like a
little easier.  You can define your own MACRO which iterates through a 
list, calls a subroutine (passed in as an argument) to format each item, 
and then returns the results as a joined string.

[% MACRO mapjoin(list, sub, joint) 
     BLOCK;
       items = [ ];
       FOR i = list;
           o = sub(i);
           items.push(o);
       END;
       items.join(joint);
     END;
%]

Then you define a MACRO which formats each item:

[% MACRO jlink(j) 
     GET '<a href="/job/' _ j.ID _ '">' _ j.Title;
%]

You need to use the (undocumented) '\' to pass a reference to the jlink
subroutine rather than calling the subroutine and passing the value.

[% mapjoin(company.Jobs, \jlink, ', ') %]

If you don't want to use the undocumented '\' (quite wise as it is likely
to be replaced by something else in TT3), then you could always pass the name
of a template block instead.

[% MACRO mapjoin(list, block, joint) 
     BLOCK;
       items = [ ];
       FOR item = list;
           out  = INCLUDE $block;
           items.push(out);
       END;
       items.join(joint);
     END;
%]
[% BLOCK jlink;
     '<a href="/job/' _ item.ID _ '">' _ item.Title;
   END;
%]
[% mapjoin(company.Jobs, 'jlink', ', ') %]

Or you can do it using a VIEW:

  [% VIEW joblinks;
       BLOCK hash;
         '<a href="/job/' _ item.ID _ '">' _ item.Title;
       END;
       BLOCK list;
         items = [ ];
         FOR i = item;
             o = view.print(i);
             items.push(o);
         END;
         items.join(', ');
       END;
     END;
  %]
  [% joblinks.print(company.Jobs) %]

Or like so:
   
  [% # base class view which knows how to join lists
     VIEW listjoin;
       BLOCK list;
         items = [ ];
         FOR i = item;
             o = view.print(i);
             items.push(o);
         END;
         items.join(', ');
       END;
     END;
  %]
  [% # subclass view which knows how to display hash arrays
     VIEW joblinks base=listjoin;
       BLOCK hash;
         '<a href="/job/' _ item.ID _ '">' _ item.Title;
       END;
     END;
  %]
  [% # another subclass view which display hash arrays differently
     VIEW joblinkslong base=listjoin;
       BLOCK hash;
         '<a href="/job/' _ item.ID _ '">' _ item.Title; blah blah
         a different block altogether yada yada, etc.
       END;
     END;
  %]

Then chose the appropriate view to print the jobs:

  [% joblinks.print(company.Jobs) %]   
or
  [% joblinkslong.print(company.Jobs) %]


Sorry for the huge answer to a simple question.  I guess the simple answer
is that there is no simple answer. :-)

HTH
A


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to