[Templates] Problem with single element arrays

Jess Robinson castaway@desert-island.me.uk
Mon, 20 Aug 2007 13:56:37 +0100 (BST)


On Mon, 20 Aug 2007, David wrote:

> Hi All,
>
> We've been using TT2 for our ecommerce application for the last couple of 
> years. Until recently we were running version 2.14 and have just upgraded to 
> the latest version (2.19). We checked the changes log for any gotcha's and 
> since there didn't seem to be any, we proceeded with the upgrade.
>
> The problem we have now is with the template treating single element arrays 
> as scalar object. For example we use the following to display the first of a 
> products images:
>
> [% product.images.first.filename %]

Is that a list of objects or hashes? If so I suggest you downgrade to 2.15 
until the next release, I've a patch in (hopefully!) that fixes it.

> which should give us the filename of the first image in the product.images 
> array. Until the recent upgrade this did work for all instances, but now, if 
> we have a product that only has one image, the template is treating it as a 
> scalar object, so 'first' returns an empty object and no value gets displayed 
> in the template.
>
> Is this a known issue, and if so is there a fix? I realise we could do 
> something like:
>
> [% IF product.images.size > 1 %]
> [% product.images.first.filename %]
> [% ELSE %]
> [% product.images.filename %]
> [% END %]

Kinda hard to do .size on something that isnt a list, and if your one-item 
is an object/hashref you'll get the number of keys, which probably isnt 
what you meant..

> however, the application we have is installed on many different sites on 
> several different servers, so making a change like this would not be trivial. 
> Any help from the list would be much appreciated.
>

Don't upgrade til 2.20..

I suggest next time you watch the RT bug list as well as the Changes file.

Jess