* 1.5.0
+ * added a dump function to help debugging templates
* added a nl2br filter
* added a random function
* added a way to change the default format for the date filter
--- /dev/null
+``dump``
+========
+
+.. versionadded:: 1.5
+ The dump function was added in Twig 1.5.
+
+The ``dump`` function dumps information about a template variable. This is
+mostly useful to debug a template that does not behave as expected by
+introspecting its variables:
+
+.. code-block:: jinja
+
+ {{ dump(user) }}
+
+.. note::
+
+ The ``debug`` function is only available when Twig ``debug`` mode is set.
+
+In an HTML context, wrap the output with a ``pre`` tag to make it easier to
+read:
+
+.. code-block:: jinja
+
+ <pre>
+ {{ dump(user) }}
+ </pre>
+
+.. tip::
+
+ Using a ``pre`` tag is not needed when `XDebug`_ is enabled and
+ ``html_errors`` is ``on``; as a bonus, the output is also nicer with
+ XDebug enabled.
+
+You can debug several variables by passing them as additional arguments:
+
+.. code-block:: jinja
+
+ {{ dump(user, categories) }}
+
+If you don't pass any value, all variables from the current context are
+dumped:
+
+.. code-block:: jinja
+
+ {{ dump() }}
+
+.. note::
+
+ Internally, Twig uses the PHP `var_dump`_ function.
+
+.. _`XDebug`: http://xdebug.org/docs/display
+.. _`var_dump`: http://php.net/var_dump
attribute
block
parent
+ dump
'escaper' => new Twig_Extension_Escaper((bool) $options['autoescape']),
'optimizer' => new Twig_Extension_Optimizer($options['optimizations']),
);
+ if ($this->debug) {
+ $this->extensions[] = new Twig_Extension_Debug();
+ }
$this->strictVariables = (bool) $options['strict_variables'];
$this->runtimeInitialized = false;
$this->setCache($options['cache']);
--- /dev/null
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2011 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Extension_Debug extends Twig_Extension
+{
+ /**
+ * Returns a list of global functions to add to the existing list.
+ *
+ * @return array An array of global functions
+ */
+ public function getFunctions()
+ {
+ // dump is safe if var_dump is overriden by xdebug
+ $isDumpOutputHtmlSafe = extension_loaded('xdebug') && get_cfg_var('xdebug.overload_var_dump') && get_cfg_var('html_errors');
+
+ return array(
+ 'dump' => new Twig_Function_Function('twig_var_dump', array('is_safe' => $isDumpOutputHtmlSafe ? array('html') : array(), 'needs_context' => true)),
+ );
+ }
+
+ /**
+ * Returns the name of the extension.
+ *
+ * @return string The extension name
+ */
+ public function getName()
+ {
+ return 'debug';
+ }
+}
+
+function twig_var_dump($context)
+{
+ ob_start();
+
+ $count = func_num_args();
+ if (1 === $count) {
+ var_dump($context);
+ } else {
+ for ($i = 1; $i < $count; $i++) {
+ var_dump(func_get_arg($i));
+ }
+ }
+
+ return ob_get_clean();
+}
--- /dev/null
+--TEST--
+"dump" function
+--TEMPLATE--
+{{ dump() }}
+{{ dump('foo') }}
+{{ dump('foo', 'bar') }}
+--DATA--
+return array('foo' => 'foo', 'bar' => 'bar')
+--CONFIG--
+return array('debug' => true, 'autoescape' => false);
+--EXPECT--
+array(2) {
+ ["foo"]=>
+ string(3) "foo"
+ ["bar"]=>
+ string(3) "bar"
+}
+
+string(3) "foo"
+
+string(3) "foo"
+string(3) "bar"
'strict_variables' => true,
), $match[2] ? eval($match[2].';') : array());
$twig = new Twig_Environment($loader, $config);
- $twig->addExtension(new Twig_Extension_Escaper());
$twig->addExtension(new TestExtension());
try {