Hi Andy,
Thanks for the detailed response. Upgrading to the SVN version of 2.20
has fixed the problem for us.
The 'products' object that we send to the template is a Class::DBI
database object. The product.images is a has_many relationship between
the products and images tables in the database. I'm not sure whether
CDBI returns an array or a reference to an array for has_many
relationships, and it's not something I would be likely to change in the
CDBI code anyway, so thankfully the upgrade has done the job.
Thanks again for your help.
Regards,
David
Andy Wardley wrote:
David wrote:
[% product.images.first.filename %]
Hi David,
Is your product.images returning a 'naked' list, like this:
return @images;
If so, you should consider changing that to return a reference to the
list, if possible:
return [EMAIL PROTECTED];
TT will try and Do The Right Thing in the first case, but it has no
way to know what the Right Thing is if you only return one item. So
it leaves it as a single item. If you return a list ref then you
always have a list ref, even if it only contains one item.
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.
What data type are the images? Are they objects, rather than, say,
hash refs? And if they are objects, are they blessed hashes or
something else?
What *should* happen is that a single item is treated as a list and
the .first vmethod is called.
I suspect the problem may have been introduced with a patch to add the
full range of 'fallback' vmethods for objects:
#------------------------------------------------------------------------
# Version 2.14a - 2nd February 2006 ## DEVELOPER RELEASE ##
#------------------------------------------------------------------------
Stash
-----
* Activated a patch in Template::Stash from Stephen Howard which adds
code for the full set of fallbacks for dot ops called against
objects which don't implement the specific method. For example
[% hashobj.vmethod %] [% listobj.2 %] and [% listobj.vmethod %] now
all work with the Perl Template::Stash. Added code to the XS Stash
to do the same and updated tests.
It's the only thing I'm aware of that might have affected it.
Hmmm... I just noticed a comment in the section for the as-yet
unreleased v2.20:
#------------------------------------------------------------------------
# Version 2.20 -
#------------------------------------------------------------------------
* Applied a patch to Template::Stash from Jess Robinson which allows you
to call a list method on a single object and have it automatically
upgraded to a single item list. Changed the XS Stash to do the same.
http://tt2.org/pipermail/templates/2006-November/009115.html
Ah yes, I think this is what you want. Here's the patch:
http://template-toolkit.org/svnweb/Template2/revision?rev=1088
And this is the altogether more-gnarly patch I made to do the same
thing for the XS Stash:
http://template-toolkit.org/svnweb/Template2/revision?rev=1090
Ah, OK, now I see why I'm confused. I'm looking at the SVN version of
the Stash code which has been updated. That's why it looks like it
*should* work from where I'm standing (and does, for me at least :-) )
So the first step would be to check it out with the latest version
from subversion.
http://tt2.org/download/index.html#svn
If that works OK then the problem's fixed and I'll push 2.20 out the
door RSN.
If that doesn't work, then let me know a bit more about what kind of
data structures your images are and we'll try and find out what's
going wrong.
Cheers
A