[Templates] Flattening a hash

Bill Moseley moseley@hank.org
Fri, 17 Feb 2006 06:13:37 -0800


On Fri, Feb 17, 2006 at 08:23:00AM +0000, Andy Wardley wrote:
> I suspect your options method is returning a list of hashes, rather than
> a reference to a list of hashes.
> 
>     return \@hashes;     # good
>     return  @hashes;     # bad
> 
> In the first case you should always get what you expect.  In the second
> case, TT will notice if you return multiple values (e.g. @hashes > 1)
> and fold it into a list reference.  But if there's only one item then
> TT will assume you meant to return that and won't fold it into a list.

Ah!  It's not TT flattening, it's TT folding.

I forgot that I'm using Rose::Object, and
Rose::Object::MakeMethods::Generic says for its array methods it does:

    Interfaces
       "get_set"
           If called with no arguments, returns the array contents in
           list context or a reference to the actual array stored by the
           object in scalar context.

So I need to to force reading the object in scalar context.

Oh, this is ugly:

        x = field.options.size > 2 ? field.options : [ field.options ];

        FOR i = x;
            "<option value='$i.value'>";
            i.label | html;
            "</option>\n";
        END;

I suspect there's something better.


-- 
Bill Moseley
moseley@hank.org