Use the json pretty print with the Twig json_encode filter

Published on 2019-08-17 • Modified on 2019-08-17

This is another example where the constant Twig function can be helpful. In this case we get the JSON_PRETTY_PRINT PHP constant to pass it to the json_encode filter. (as the second argument of course, the first is obviously the variable to display)


{{ article|json_encode(constant('JSON_PRETTY_PRINT')) }}
Code execution output:
{
    "type": "Snippet",
    "name": "Use the json pretty print with the Twig json_encode filter",
    "author": "COil",
    "date_published": {
        "date": "2019-08-17 00:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe\/Paris"
    },
    "date_modified": {
        "date": "2019-08-17 00:00:00.000000",
        "timezone_type": 3,
        "timezone": "Europe\/Paris"
    },
    "publisher": "Strangebuzz (1)",
    "in_language": "fr,en",
    "keyword": "twig,symfony,symfony4,json",
    "timeRequired": "1"
}

 More on Stackoverflow   Read the doc


» Comments

Privacy-focused with Commento. (Comment system added on 2019-11-22: be the first! 🥇)