Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``dump`` |
| 2 | ======== |
| 3 | |
| 4 | The ``dump`` function dumps information about a template variable. This is |
| 5 | mostly useful to debug a template that does not behave as expected by |
| 6 | introspecting its variables: |
| 7 | |
| 8 | .. code-block:: twig |
| 9 | |
| 10 | {{ dump(user) }} |
| 11 | |
| 12 | .. note:: |
| 13 | |
| 14 | The ``dump`` function is not available by default. You must add the |
| 15 | ``\Twig\Extension\DebugExtension`` extension explicitly when creating your Twig |
| 16 | environment:: |
| 17 | |
| 18 | $twig = new \Twig\Environment($loader, [ |
| 19 | 'debug' => true, |
| 20 | // ... |
| 21 | ]); |
| 22 | $twig->addExtension(new \Twig\Extension\DebugExtension()); |
| 23 | |
| 24 | Even when enabled, the ``dump`` function won't display anything if the |
| 25 | ``debug`` option on the environment is not enabled (to avoid leaking debug |
| 26 | information on a production server). |
| 27 | |
| 28 | In an HTML context, wrap the output with a ``pre`` tag to make it easier to |
| 29 | read: |
| 30 | |
| 31 | .. code-block:: html+twig |
| 32 | |
| 33 | <pre> |
| 34 | {{ dump(user) }} |
| 35 | </pre> |
| 36 | |
| 37 | .. tip:: |
| 38 | |
| 39 | Using a ``pre`` tag is not needed when `XDebug`_ is enabled and |
| 40 | ``html_errors`` is ``on``; as a bonus, the output is also nicer with |
| 41 | XDebug enabled. |
| 42 | |
| 43 | You can debug several variables by passing them as additional arguments: |
| 44 | |
| 45 | .. code-block:: twig |
| 46 | |
| 47 | {{ dump(user, categories) }} |
| 48 | |
| 49 | If you don't pass any value, all variables from the current context are |
| 50 | dumped: |
| 51 | |
| 52 | .. code-block:: twig |
| 53 | |
| 54 | {{ dump() }} |
| 55 | |
| 56 | .. note:: |
| 57 | |
| 58 | Internally, Twig uses the PHP `var_dump`_ function. |
| 59 | |
| 60 | Arguments |
| 61 | --------- |
| 62 | |
| 63 | * ``context``: The context to dump |
| 64 | |
| 65 | .. _`XDebug`: https://xdebug.org/docs/display |
| 66 | .. _`var_dump`: https://secure.php.net/var_dump |