From 1bdfacee11393f18563ccd8143a9408f07449ede Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Tue, 6 Aug 2013 17:35:29 +0200 Subject: [PATCH] =?utf8?q?Template:=20as=5Flist-Methode=20zum=20For=C3=A7i?= =?utf8?q?eren=20vom=20Listenkontext?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/Template/Plugin/HTMLFixes.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/SL/Template/Plugin/HTMLFixes.pm b/SL/Template/Plugin/HTMLFixes.pm index fad69932a..ce21b5cd9 100644 --- a/SL/Template/Plugin/HTMLFixes.pm +++ b/SL/Template/Plugin/HTMLFixes.pm @@ -1,6 +1,7 @@ package SL::Template::Plugin::HTMLFixes; use Template::Plugin::HTML; +use Template::Stash; 1; @@ -27,3 +28,19 @@ sub url { } 1; + +package Template::Stash; + +# A method for forcing list context. If a method uses 'wantarray' then +# calling that method from Template will do strange stuff like chosing +# scalar context. The most obvious offender are RDBO relationships. + +# Example of how NOT to test whether or not a customer has contacts: +# [% IF customer.contacts.size %] ... +# Instead force list context and then test the size: +# [% IF customer.contacts.as_list.size %] ... +$Template::Stash::LIST_OPS->{ as_list } = sub { + return ref( $_[0] ) eq 'ARRAY' ? shift : [shift]; +}; + +1; -- 2.20.1