Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``json_encode`` |
| 2 | =============== |
| 3 | |
| 4 | The ``json_encode`` filter returns the JSON representation of a value: |
| 5 | |
| 6 | .. code-block:: twig |
| 7 | |
| 8 | {{ data|json_encode() }} |
| 9 | |
| 10 | .. note:: |
| 11 | |
| 12 | Internally, Twig uses the PHP `json_encode`_ function. |
| 13 | |
| 14 | Arguments |
| 15 | --------- |
| 16 | |
| 17 | * ``options``: A bitmask of `json_encode options`_: ``{{ |
| 18 | data|json_encode(constant('JSON_PRETTY_PRINT')) }}``. |
| 19 | Combine constants using :ref:`bitwise operators<template_logic>`: |
| 20 | ``{{ data|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_HEX_QUOT')) }}`` |
| 21 | |
| 22 | .. _`json_encode`: https://secure.php.net/json_encode |
| 23 | .. _`json_encode options`: https://secure.php.net/manual/en/json.constants.php |