0
votes

I was wondering if I could get some help with how I have this syntax error please as I am fairly new to Twig.

This is my array:

{% set bookings = {
      "Tuesday": {
        "1315", // this line is causing the error
        "1330",
        "1345",
        "1430",
        "1445",
        "1460",
        "1515",
        "1530",
        "1545",
        "1630",
        "1715",
        "1730",
        "1745",
        "1815",
        "1830",
        "1845"
      },
      "Wednesday" : {
        "0930",
        "0945",
        "1015",
        "1030",
        "1045",
        "1115",
        "1130",
        "1215",
        "1230",
        "1245",
        "1415",
        "1445",
        "1530",
        "1630",
        "1645",
        "1815",
        "1830"
      },
      "Thursday": {
        "0900",
        "0915",
        "0930",
        "0945",
        "1000",
        "1015",
        "1030",
        "1045",
        "1100",
        "1115",
        "1130",
        "1145",
        "1200",
        "1215",
        "1230",
        "1245",
        "1300",
        "1315",
        "1330",
        "1345",
        "1400",
        "1415",
        "1430",
        "1445",
        "1500",
        "1515",
        "1530",
        "1545",
        "1715",
        "1730",
        "1745"
      },
      "Friday" : {
        "1015",
        "1030",
        "1045",
        "1215",
        "1230",
        "1245",
        "1430",
        "1445"
      }
    } %}

The error message I am getting is:

PHP Fatal error: Uncaught Twig\Error\SyntaxError: A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "," ("punctuation" expected with value ":").

I am sure it is something fairly simple, but it doesn't seem to be something that I can find in their documentation - the closest I got was a page describing how to set variables, but it's not that in depth.

Thank you.

1
You should use the array initializer ([1,2, ...]) instead of the object initializer ({ 'a': 1, 'b': 2, ...}), which is what the error is saying (you're missing 'a': in your list). - Jared Farrish

1 Answers

4
votes

Twig hashes, wrapped with { }, are maps from keys to values. Since your nested structure is just a list of values, you should be using arrays, wrapped with [ ]:

{% set bookings = { 
  "Tuesday": [ 
    "1315",
    "1330",
    ...
  ],
  "Wednesday": [
    "0930",
    ...
  ],
  ...
 } %}