From: Sven Schöling Date: Wed, 12 Oct 2011 14:55:09 +0000 (+0200) Subject: dot notation in templates auch für TEMPLATE_ARRAY variablen X-Git-Tag: release-2.7.0beta1~232 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=85629633fb54db27d018fe8ae3319257659683c3;p=kivitendo-erp.git dot notation in templates auch für TEMPLATE_ARRAY variablen --- diff --git a/SL/Template/Simple.pm b/SL/Template/Simple.pm index 8ad622043..9960908da 100644 --- a/SL/Template/Simple.pm +++ b/SL/Template/Simple.pm @@ -85,27 +85,15 @@ sub uses_temp_file { } sub _get_loop_variable { - my $self = shift; - my $var = shift; - my $get_array = shift; - my @indices = @_; - + my ($self, $var, $get_array, @indices) = @_; my $form = $self->{form}; - my $value; + my ($value, @methods); if ($var =~ m/\./) { - $value = $form; - for my $part (split(m/\./, $var)) { - if (ref($value) =~ m/^(?:Form|HASH)$/) { - $value = $value->{$part}; - } elsif (blessed($value) && $value->can($part)) { - $value = $value->$part; - } else { - $value = ''; - last; - } - } - } elsif (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) { + ($var, @methods) = split m/\./, $var; + } + + if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) { $value = $form->{TEMPLATE_ARRAYS}->{$var}; } else { $value = $form->{$var}; @@ -116,6 +104,17 @@ sub _get_loop_variable { $value = $value->[$indices[$i]]; } + for my $part (@methods) { + if (ref($value) =~ m/^(?:Form|HASH)$/) { + $value = $value->{$part}; + } elsif (blessed($value) && $value->can($part)) { + $value = $value->$part; + } else { + $value = ''; + last; + } + } + return $value; } diff --git a/t/helper/template/simple.t b/t/helper/template/simple.t new file mode 100644 index 000000000..056fb8be4 --- /dev/null +++ b/t/helper/template/simple.t @@ -0,0 +1,55 @@ +#!/usr/bin/perl + +use strict; +use lib 't'; + +use DateTime; +use Test::More; + +use_ok qw(SL::Template::Simple); + +my $t = SL::Template::Simple->new(form => {}); + +sub test ($$$$$) { + my ($form, $template_array, $query, $result, $text) = @_; + + $t->{form} = bless $form, 'Form'; + $t->{form}->{TEMPLATE_ARRAYS} = $template_array if $template_array; + is_deeply $t->_get_loop_variable(@$query), $result, $text; +} + +test { a => 1 }, {}, [ 'a', 0 ], 1, 'simple access'; +test { }, { a => [ 1 ] }, [ 'a', 0, 0 ], 1, 'template access'; +test { }, { a => [ 1..4 ] }, [ 'a', 0, 3 ], 4, 'template access > 1'; +test { }, { a => [ [ 1 ] ] }, [ 'a', 0, 0, 0 ], 1, 'template access more than one layer'; +test { }, { a => [ 1 ] }, [ 'a', 0, 3 ], undef, 'short circuit if array is missing'; +test { a => 2 }, { a => [ 1 ] }, [ 'a', 0 ], 2, 'no template access ignores templates'; +test { a => 2 }, { a => [ 1 ] }, [ 'a', 1 ], [ 1 ], 'array access returns array'; + +test { a => 2, TEMPLATE_ARRAY => [ a => [1] ] }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored'; +test { a => 2, TEMPLATE_ARRAY => 1 }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored 2'; + +test { a => { b => 2 }, 'a.b' => 5 }, {}, [ 'a.b', 0 ], 2, 'dot access'; +test { a => { b => { c => 5 } } }, {}, [ 'a.b.c', 0 ], 5, 'deep dot access'; +test { a => { b => 2 } }, {}, [ 'a.b', 1 ], 2, 'dot access ignores array'; +test { a => { b => 2 } }, { 'a.b' => 3 }, [ 'a.b', 0, 0 ], 2, 'dot access ignores template'; + +{ package LXOTestDummy; sub b { 5 } } +my $o = bless [], 'LXOTestDummy'; + +test { 'a.b' => 2, a => $o }, {}, [ 'a.b', 0 ], 5, 'dot object access'; +test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.b', 0 ], 5, 'deep dot object access'; +test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.c', 0 ], undef, 'dot hash does not shortcut'; +test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.c', 0 ], '', 'dot object shortcuts to empty string'; + +test {}, { a => [ { b => 2 } ], 'a.b' => 5 }, [ 'a.b', 0, 0 ], 2, 'array dot access'; +test {}, { a => [ { b => { c => 5 } } ] }, [ 'a.b.c', 0, 0 ], 5, 'array deep dot access'; +test {}, { a => [ { b => 2 } ] }, [ 'a.b', 1, 0 ], 2, 'array dot access ignores array'; +test { 'a.b' => 3 }, { a => [ { b => 2 } ] }, , [ 'a.b', 0, 0 ], 2, 'array dot access ignores template'; + +test {}, { a => [ $o ] }, [ 'a.b', 0, 0 ], 5, 'array dot object access'; +test {}, { a => [ { b => $o } ] }, [ 'a.b.b', 0, 0 ], 5, 'array deep dot object access'; +test {}, { a => [ { b => $o } ] }, [ 'a.c', 0, 0 ], undef, 'array dot hash does not shortcut'; +test {}, { a => [ { b => $o } ] }, [ 'a.b.c', 0, 0 ], '', 'array dot object shortcuts to empty string'; + +done_testing();