1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
--TEST--
Twig supports array notation
--TEMPLATE--
{# empty array #}
{{ []|join(',') }}
{{ [1, 2]|join(',') }}
{{ ['foo', "bar"]|join(',') }}
{{ {0: 1, 'foo': 'bar'}|join(',') }}
{{ {0: 1, 'foo': 'bar'}|keys|join(',') }}
{{ {0: 1, foo: 'bar'}|join(',') }}
{{ {0: 1, foo: 'bar'}|keys|join(',') }}
{# nested arrays #}
{% set a = [1, 2, [1, 2], {'foo': {'foo': 'bar'}}] %}
{{ a[2]|join(',') }}
{{ a[3]["foo"]|join(',') }}
{# works even if [] is used inside the array #}
{{ [foo[bar]]|join(',') }}
{# elements can be any expression #}
{{ ['foo'|upper, bar|upper, bar == foo]|join(',') }}
{# arrays can have a trailing , like in PHP #}
{{
[
1,
2,
]|join(',')
}}
{# keys can be any expression #}
{% set a = 1 %}
{% set b = "foo" %}
{% set markup_instance %}fooe{% endset %}
{% set ary = { (a): 'a', (b): 'b', 'c': 'c', (a ~ b): 'd', (markup_instance): 'e' } %}
{{ ary|keys|join(',') }}
{{ ary|join(',') }}
{# ArrayAccess #}
{{ array_access['a'] }}
{# ObjectStorage #}
{{ object_storage[object] }}
{{ object_storage[object_storage]|default('bar') }}
{# array that does not exist #}
{{ does_not_exist[0]|default('ok') }}
{{ does_not_exist[0].does_not_exist_either|default('ok') }}
{{ does_not_exist[0]['does_not_exist_either']|default('ok') }}
{# indexes are kept #}
{% set trad = {194:'ABC',141:'DEF',100:'GHI',170:'JKL',110:'MNO',111:'PQR'} %}
{% set trad2 = {'194':'ABC','141':'DEF','100':'GHI','170':'JKL','110':'MNO','111':'PQR'} %}
{{ trad == trad2 ? 'OK' : 'KO' }}
{# indexes are kept #}
{{ { 1: "first", 0: "second" } == { '1': "first", '0': "second" } ? 'OK' : 'KO' }}
{{ { 1: "first", 0: "second" } == indices_1 ? 'OK' : 'KO' }}
{{ { 1: "first", 'foo': "second", 2: "third" } == { '1': "first", 'foo': "second", '2': "third" } ? 'OK' : 'KO' }}
{{ { 1: "first", 'foo': "second", 2: "third" } == indices_2 ? 'OK' : 'KO' }}
--DATA--
$objectStorage = new SplObjectStorage();
$object = new stdClass();
$objectStorage[$object] = 'foo';
return [
'bar' => 'bar',
'foo' => ['bar' => 'bar'],
'array_access' => new \ArrayObject(['a' => 'b']),
'object_storage' => $objectStorage,
'object' => $object,
'indices_1' => [ 1 => 'first', 0 => 'second' ],
'indices_2' => [ 1 => 'first', 'foo' => 'second', 2 => 'third' ],
]
--EXPECT--
1,2
foo,bar
1,bar
0,foo
1,bar
0,foo
1,2
bar
bar
FOO,BAR,
1,2
1,foo,c,1foo,fooe
a,b,c,d,e
b
foo
bar
ok
ok
ok
OK
OK
OK
OK
OK
--DATA--
return [
'bar' => 'bar',
'foo' => ['bar' => 'bar'],
'array_access' => new \ArrayObject(['a' => 'b']),
'object' => new stdClass(),
'indices_1' => [ 1 => 'first', 0 => 'second' ],
'indices_2' => [ 1 => 'first', 'foo' => 'second', 2 => 'third' ],
]
--CONFIG--
return ['strict_variables' => false]
--EXPECT--
1,2
foo,bar
1,bar
0,foo
1,bar
0,foo
1,2
bar
bar
FOO,BAR,
1,2
1,foo,c,1foo,fooe
a,b,c,d,e
b
bar
ok
ok
ok
OK
OK
OK
OK
OK
|