dump.rst 1.5KB

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