Revision 1bdfacee
Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt
SL/Template/Plugin/HTMLFixes.pm | ||
---|---|---|
1 | 1 |
package SL::Template::Plugin::HTMLFixes; |
2 | 2 |
|
3 | 3 |
use Template::Plugin::HTML; |
4 |
use Template::Stash; |
|
4 | 5 |
|
5 | 6 |
1; |
6 | 7 |
|
... | ... | |
27 | 28 |
} |
28 | 29 |
|
29 | 30 |
1; |
31 |
|
|
32 |
package Template::Stash; |
|
33 |
|
|
34 |
# A method for forcing list context. If a method uses 'wantarray' then |
|
35 |
# calling that method from Template will do strange stuff like chosing |
|
36 |
# scalar context. The most obvious offender are RDBO relationships. |
|
37 |
|
|
38 |
# Example of how NOT to test whether or not a customer has contacts: |
|
39 |
# [% IF customer.contacts.size %] ... |
|
40 |
# Instead force list context and then test the size: |
|
41 |
# [% IF customer.contacts.as_list.size %] ... |
|
42 |
$Template::Stash::LIST_OPS->{ as_list } = sub { |
|
43 |
return ref( $_[0] ) eq 'ARRAY' ? shift : [shift]; |
|
44 |
}; |
|
45 |
|
|
46 |
1; |
Auch abrufbar als: Unified diff
Template: as_list-Methode zum Forçieren vom Listenkontext