Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 85629633

Von Sven Schöling vor etwa 13 Jahren hinzugefügt

  • ID 85629633fb54db27d018fe8ae3319257659683c3
  • Vorgänger db7c3a52
  • Nachfolger d729e328

dot notation in templates auch für TEMPLATE_ARRAY variablen

Unterschiede anzeigen:

SL/Template/Simple.pm
85 85
}
86 86

  
87 87
sub _get_loop_variable {
88
  my $self      = shift;
89
  my $var       = shift;
90
  my $get_array = shift;
91
  my @indices   = @_;
92

  
88
  my ($self, $var, $get_array, @indices) = @_;
93 89
  my $form      = $self->{form};
94
  my $value;
90
  my ($value, @methods);
95 91

  
96 92
  if ($var =~ m/\./) {
97
    $value = $form;
98
    for my $part (split(m/\./, $var)) {
99
      if (ref($value) =~ m/^(?:Form|HASH)$/) {
100
        $value = $value->{$part};
101
      } elsif (blessed($value) && $value->can($part)) {
102
        $value = $value->$part;
103
      } else {
104
        $value = '';
105
        last;
106
      }
107
    }
108
  } elsif (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
93
    ($var, @methods) = split m/\./, $var;
94
  }
95

  
96
  if (($get_array || @indices) && (ref $form->{TEMPLATE_ARRAYS} eq 'HASH') && (ref $form->{TEMPLATE_ARRAYS}->{$var} eq 'ARRAY')) {
109 97
    $value = $form->{TEMPLATE_ARRAYS}->{$var};
110 98
  } else {
111 99
    $value = $form->{$var};
......
116 104
    $value = $value->[$indices[$i]];
117 105
  }
118 106

  
107
  for my $part (@methods) {
108
    if (ref($value) =~ m/^(?:Form|HASH)$/) {
109
      $value = $value->{$part};
110
    } elsif (blessed($value) && $value->can($part)) {
111
      $value = $value->$part;
112
    } else {
113
      $value = '';
114
      last;
115
    }
116
  }
117

  
119 118
  return $value;
120 119
}
121 120

  
t/helper/template/simple.t
1
#!/usr/bin/perl
2

  
3
use strict;
4
use lib 't';
5

  
6
use DateTime;
7
use Test::More;
8

  
9
use_ok qw(SL::Template::Simple);
10

  
11
my $t = SL::Template::Simple->new(form => {});
12

  
13
sub test ($$$$$) {
14
  my ($form, $template_array, $query, $result, $text) = @_;
15

  
16
  $t->{form} = bless $form, 'Form';
17
  $t->{form}->{TEMPLATE_ARRAYS} = $template_array  if $template_array;
18
  is_deeply $t->_get_loop_variable(@$query), $result, $text;
19
}
20

  
21
test { a => 1 }, {}, [ 'a', 0 ], 1, 'simple access';
22
test { }, { a => [ 1 ] }, [ 'a', 0, 0 ], 1, 'template access';
23
test { }, { a => [ 1..4 ] }, [ 'a', 0, 3 ], 4, 'template access > 1';
24
test { }, { a => [ [ 1 ] ] }, [ 'a', 0, 0, 0 ], 1, 'template access more than one layer';
25
test { }, { a => [ 1 ] }, [ 'a', 0, 3 ], undef, 'short circuit if array is missing';
26
test { a => 2 }, { a => [ 1 ] }, [ 'a', 0 ], 2, 'no template access ignores templates';
27
test { a => 2 }, { a => [ 1 ] }, [ 'a', 1 ], [ 1 ], 'array access returns array';
28

  
29
test { a => 2, TEMPLATE_ARRAY => [ a => [1] ] }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored';
30
test { a => 2, TEMPLATE_ARRAY => 1 }, undef, [ 'a', 0, 0 ], 2 , 'wrong template_array gets ignored 2';
31

  
32
test { a => { b => 2 }, 'a.b' => 5 }, {}, [ 'a.b', 0 ], 2, 'dot access';
33
test { a => { b => { c => 5 } } }, {}, [ 'a.b.c', 0 ], 5, 'deep dot access';
34
test { a => { b => 2 } }, {}, [ 'a.b', 1 ], 2, 'dot access ignores array';
35
test { a => { b => 2 } }, { 'a.b' => 3 }, [ 'a.b', 0, 0 ], 2, 'dot access ignores template';
36

  
37
{ package LXOTestDummy; sub b { 5 } }
38
my $o = bless [], 'LXOTestDummy';
39

  
40
test { 'a.b' => 2, a => $o }, {}, [ 'a.b', 0 ], 5, 'dot object access';
41
test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.b', 0 ], 5, 'deep dot object access';
42
test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.c', 0 ], undef, 'dot hash does not shortcut';
43
test { 'a.b.b' => 2, a => { b => $o } }, {}, [ 'a.b.c', 0 ], '', 'dot object shortcuts to empty string';
44

  
45
test {}, { a => [ { b => 2 } ], 'a.b' => 5 },  [ 'a.b', 0, 0 ], 2, 'array dot access';
46
test {}, { a => [ { b => { c => 5 } } ] },  [ 'a.b.c', 0, 0 ], 5, 'array deep dot access';
47
test {}, { a => [ { b => 2 } ] }, [ 'a.b', 1, 0 ], 2, 'array dot access ignores array';
48
test { 'a.b' => 3 }, { a => [ { b => 2 } ] }, , [ 'a.b', 0, 0 ], 2, 'array dot access ignores template';
49

  
50
test {}, { a => [ $o ] },  [ 'a.b', 0, 0 ], 5, 'array dot object access';
51
test {}, { a => [ { b => $o } ] }, [ 'a.b.b', 0, 0 ], 5, 'array deep dot object access';
52
test {}, { a => [ { b => $o } ] },  [ 'a.c', 0, 0 ], undef, 'array dot hash does not shortcut';
53
test {}, { a => [ { b => $o } ] },  [ 'a.b.c', 0, 0 ], '', 'array dot object shortcuts to empty string';
54

  
55
done_testing();

Auch abrufbar als: Unified diff