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