dump.rst 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ``debug`` function is not available by default. You must load it explicitly::
  12. $twig = new Twig_Environment($loader, $config);
  13. $twig->addExtension(new Twig_Extension_Debug());
  14. Even when loaded explicitly, it won't do anything if the ``debug`` option
  15. is not enabled.
  16. In an HTML context, wrap the output with a ``pre`` tag to make it easier to
  17. read:
  18. .. code-block:: jinja
  19. <pre>
  20. {{ dump(user) }}
  21. </pre>
  22. .. tip::
  23. Using a ``pre`` tag is not needed when `XDebug`_ is enabled and
  24. ``html_errors`` is ``on``; as a bonus, the output is also nicer with
  25. XDebug enabled.
  26. You can debug several variables by passing them as additional arguments:
  27. .. code-block:: jinja
  28. {{ dump(user, categories) }}
  29. If you don't pass any value, all variables from the current context are
  30. dumped:
  31. .. code-block:: jinja
  32. {{ dump() }}
  33. .. note::
  34. Internally, Twig uses the PHP `var_dump`_ function.
  35. .. _`XDebug`: http://xdebug.org/docs/display
  36. .. _`var_dump`: http://php.net/var_dump