array.test 1022B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. --TEST--
  2. Twig supports array notation
  3. --TEMPLATE--
  4. {# empty array #}
  5. {{ []|join(',') }}
  6. {{ [1, 2]|join(',') }}
  7. {{ ['foo', "bar"]|join(',') }}
  8. {{ {0: 1, 'foo': 'bar'}|join(',') }}
  9. {{ {0: 1, 'foo': 'bar'}|keys|join(',') }}
  10. {{ {0: 1, foo: 'bar'}|join(',') }}
  11. {{ {0: 1, foo: 'bar'}|keys|join(',') }}
  12. {# nested arrays #}
  13. {% set a = [1, 2, [1, 2], {'foo': {'foo': 'bar'}}] %}
  14. {{ a[2]|join(',') }}
  15. {{ a[3]["foo"]|join(',') }}
  16. {# works even if [] is used inside the array #}
  17. {{ [foo[bar]]|join(',') }}
  18. {# elements can be any expression #}
  19. {{ ['foo'|upper, bar|upper, bar == foo]|join(',') }}
  20. {# arrays can have a trailing , like in PHP #}
  21. {{
  22. [
  23. 1,
  24. 2,
  25. ]|join(',')
  26. }}
  27. {# keys can be any expression #}
  28. {% set a = 1 %}
  29. {% set b = "foo" %}
  30. {% set ary = { (a): 'a', (b): 'b', 'c': 'c', (a ~ b): 'd' } %}
  31. {{ ary|keys|join(',') }}
  32. {{ ary|join(',') }}
  33. --DATA--
  34. return array('bar' => 'bar', 'foo' => array('bar' => 'bar'))
  35. --EXPECT--
  36. 1,2
  37. foo,bar
  38. 1,bar
  39. 0,foo
  40. 1,bar
  41. 0,foo
  42. 1,2
  43. bar
  44. bar
  45. FOO,BAR,
  46. 1,2
  47. 1,foo,c,1foo
  48. a,b,c,d