in reply to Template, sorting an array with only one member

Code talks louder than words :)
$ cat sort-list.tt2 [% MACRO blocker BLOCK %] [% FOREACH element IN array.sort('name') %] [% element.name %] [%- END -%] [%- END -%] [%- SET array = [ { name => 'foo', value => 1 }, { name => 'bar', value => 2 }, { name => 'baz', value => 3 } ]; blocker %] [%- SET array = [ { name => 'bar', value => 2 }, ]; blocker %] $ tpage sort-list.tt2 bar baz foo bar

My code says it works ... so what does your code say?

  • Comment on Re: Template, sorting an array with only one member (code not words)
  • Download Code

Replies are listed 'Best First'.
Re^2: Template, sorting an array with only one member (code not words)
by FloydATC (Deacon) on Oct 20, 2014 at 11:07 UTC

    I get the same result if I define the array within the template. If the array is served as a reference through Template::process() then it doesn't work as expected.

    -- FloydATC

    Time flies when you don't know what you're doing

      I get the same result if I define the array within the template. If the array is served as a reference through Template::process() then it doesn't work as expected.

      Thats just more words :) but I'm glad to see choroba got you to play ;) and you figured it out

Re^2: Template, sorting an array with only one member
by Anonymous Monk on Oct 20, 2014 at 11:05 UTC

    You must be giving it a hash instead of an array ... don't know how you managed to write that :)

    [% MACRO blocker BLOCK %] [% FOREACH element IN array.sort('name') %] [% element.name %] [%- END -%] [%- END -%] [%- SET array = [ { name => 'foo', value => 1 }, { name => 'bar', value => 2 }, { name => 'baz', value => 3 } ]; blocker; %] [%- SET array = [ { name => 'bar', value => 2 }, ]; blocker; %] [%- ## A HASH IS NOT AN ARRAY OF HASHES SET array = { name => 'bar', value => 2 }; blocker; %]

    yadayadayada

    * bar * baz * foo * bar * *