]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | ``dump`` |
2 | ======== | |
3 | ||
4 | .. versionadded:: 1.5 | |
5 | The dump function was added in Twig 1.5. | |
6 | ||
7 | The ``dump`` function dumps information about a template variable. This is | |
8 | mostly useful to debug a template that does not behave as expected by | |
9 | introspecting 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_Debug`` extension explicitly when creating your Twig | |
19 | environment:: | |
20 | ||
21 | $twig = new Twig_Environment($loader, array( | |
22 | 'debug' => true, | |
23 | // ... | |
24 | )); | |
25 | $twig->addExtension(new Twig_Extension_Debug()); | |
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 | ||
31 | In an HTML context, wrap the output with a ``pre`` tag to make it easier to | |
32 | read: | |
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 | ||
46 | You can debug several variables by passing them as additional arguments: | |
47 | ||
48 | .. code-block:: jinja | |
49 | ||
50 | {{ dump(user, categories) }} | |
51 | ||
52 | If you don't pass any value, all variables from the current context are | |
53 | dumped: | |
54 | ||
55 | .. code-block:: jinja | |
56 | ||
57 | {{ dump() }} | |
58 | ||
59 | .. note:: | |
60 | ||
61 | Internally, Twig uses the PHP `var_dump`_ function. | |
62 | ||
63 | Arguments | |
64 | --------- | |
65 | ||
66 | * ``context``: The context to dump | |
67 | ||
68 | .. _`XDebug`: http://xdebug.org/docs/display | |
69 | .. _`var_dump`: http://php.net/var_dump |